In the article, I talked a lot about the implementation method and principle of PHP calculation time interval implementation program code and analyzed how to do this function. Students who need it can take a closer look.
The following example tells us how many seconds ago a post was posted in the forum and other functions,
Analysis
Real Time PHP
1 second 2 "seconds"
_____________________
30 seconds==60
5 minutes==60*10 //I think it should be five minutes here, not one minute. Personally, I think one minute should be 60*2
10 minutes==60*20
30 minutes==60*60
1 hour==60*60*2
2.5 hours==60*60*5
Principle
Use the current time (time()), subtract the new time when you enter the information (note that it is the timestamp), and the difference obtained is the number of seconds, and then convert it according to your own needs. For example, if you want to convert to minutes, divide by 60.
Example
The code is as follows |
Copy code |
代码如下 |
复制代码 |
/**
* 时间差计算
*
* @param Timestamp $time
* @return String Time Elapsed
* @author Shelley Shyan
* @copyright http://www.bKjia.c0m (Professional PHP Architecture)
*/
function time2Units ($time)
{
$year = floor($time / 60 / 60 / 24 / 365);
$time -= $year * 60 * 60 * 24 * 365;
$month = floor($time / 60 / 60 / 24 / 30);
$time -= $month * 60 * 60 * 24 * 30;
$week = floor($time / 60 / 60 / 24 / 7);
$time -= $week * 60 * 60 * 24 * 7;
$day = floor($time / 60 / 60 / 24);
$time -= $day * 60 * 60 * 24;
$hour = floor($time / 60 / 60);
$time -= $hour * 60 * 60;
$minute = floor($time / 60);
$time -= $minute * 60;
$second = $time;
$elapse = '';
$unitArr = array('年' =>'year', '个月'=>'month', '周'=>'week', '天'=>'day',
'小时'=>'hour', '分钟'=>'minute', '秒'=>'second'
);
foreach ( $unitArr as $cn => $u )
{
if ( $$u > 0 )
{
$elapse = $$u . $cn;
break;
}
}
return $elapse;
}
$past = 2052345678; // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past;
echo '发表于' . time2Units($diff) . '前';
?>
|
/**<🎜>
* Time difference calculation<🎜>
*<🎜>
* @param Timestamp $time<🎜>
* @return String Time Elapsed<🎜>
* @author Shelley Shyan<🎜>
* @copyright http://www.bKjia.c0m (Professional PHP Architecture)<🎜>
*/<🎜>
function time2Units ($time)<🎜>
{<🎜>
$year = floor($time / 60 / 60 / 24 / 365);<🎜>
$time -= $year * 60 * 60 * 24 * 365;<🎜>
$month = floor($time / 60 / 60 / 24 / 30);<🎜>
$time -= $month * 60 * 60 * 24 * 30;<🎜>
$week = floor($time / 60 / 60 / 24 / 7);<🎜>
$time -= $week * 60 * 60 * 24 * 7;<🎜>
$day = floor($time / 60 / 60 / 24);<🎜>
$time -= $day * 60 * 60 * 24;<🎜>
$hour = floor($time / 60 / 60);<🎜>
$time -= $hour * 60 * 60;<🎜>
$minute = floor($time / 60);<🎜>
$time -= $minute * 60;<🎜>
$second = $time;<🎜>
$elapse = '';<🎜>
<🎜> $unitArr = array('year' =>'year', 'month'=>'month', 'week'=>'week', 'day'=>'day',
'Hour'=>'hour', 'Minute'=>'minute', 'Second'=>'second'
);
foreach ( $unitArr as $cn => $u )
{
If ( $$u > 0 )
{
$elapse = $$u . $cn;
break;
}
}
return $elapse;
}
$past = 2052345678; // Some timestamp in the past
$now = time(); // Current timestamp
$diff = $now - $past;
echo 'published on' . time2Units($diff) . 'before';
?>
|
This is written by a student who is just starting out
The code is as follows
代码如下 |
复制代码 |
$regist1 = "05/12/2006";
$regist2 = "10/05/2007";
list($month1,$day1,$year1) = explode("/",$regist1);
list($month2,$day2,$year2) = explode("/",$regist2);
$regist1 = mktime(0,0,0,$month1,$day1,$year1);
$regist2 = mktime(0,0,0,$month2,$day2,$year2);
$time_difference = $regist2-$regist1;
echo ("时间差:");
echo date("Y",$time_difference) - 1970;
echo ("年");
echo date("m",$time_difference) - 1;
echo ("个月");
echo date("d",$time_difference) - 1;
echo ("天");
?>
|
|
Copy code
|
$regist1 = "05/12/2006"; |
$regist2 = "10/05/2007";
list($month1,$day1,$year1) = explode("/",$regist1);
List($month2,$day2,$year2) = explode("/",$regist2);
$regist1 = mktime(0,0,0,$month1,$day1,$year1);
$regist2 = mktime(0,0,0,$month2,$day2,$year2);
$time_difference = $regist2-$regist1;
echo ("Time difference:");
echo date("Y",$time_difference) - 1970;
echo ("year");
echo date("m",$time_difference) - 1;
echo ("month");
echo date("d",$time_difference) - 1;
echo ("天");
?>
Well, if you like it, just choose to use it.
http://www.bkjia.com/PHPjc/631674.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631674.htmlTechArticleIn the article, I talked a lot about the principles of the PHP calculation time interval implementation program code and how to do the analysis Students who need to use this function can take a closer look. Below...