Home > Article > Backend Development > Timeline development in PHP, that is, displayed as "just now", "5 minutes ago", "yesterday 10:23"_PHP Tutorial
//Time conversion function
function tranTime($time) {
$rtime = date("m-d H:i",$time);
$htime = date("H:i",$time);
$time = time() - $time;
If ($time < 60) {
$str = 'just';
}
elseif ($time < 60 * 60) {
$min = floor($time/60);
$str = $min.'minutes ago';
}
elseif ($time < 60 * 60 * 24) {
$h = floor($time/(60*60));
$str = $h.'Hours ago'.$htime;
}
elseif ($time < 60 * 60 * 24 * 3) {
$d = floor($time/(60*60*24));
If($d==1)
$str = 'yesterday'.$rtime;
else
$str = 'The day before yesterday'.$rtime;
}
else {
$str = $rtime;
}
Return $str;
}
The parameter $time in the function tranTime() must be a Unix timestamp. If not, please use strtotime() to convert it to a Unix timestamp first.
Call the function and output directly:
$times="1286861696 ";
echo tranTime($times);
Author: aolinks's Blog