This article will introduce how to implement time-based conversion.
First we need to understand several functions of time:
time(): Returns the current Unix timestamp
date(): Format a local time/date.
Application example:
date("Y-m-d H:i:s",time()); //Format the current time, output: 2011-9-24 07:27:35
strtotime(): parses the date-time description of any English text into a Unix timestamp.
Application example:
echo strtotime("+1 day"), "\n"; //Output timestamp 1 day ago: 131693222
date_default_timezone_set(): Set the default time zone to use.
Generally, we set the Beijing time: date_default_timezone_set("PRC");
After understanding the above functions, let’s write the timeline function:
The principle of this function is to compare the current time of the system with the target time, obtain a difference, and then compare the difference with the time range (converted into seconds), and output different results according to the range in the time axis (such as: 5 minutes ago). For ease of calculation, we convert all times to Unix timestamps.
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.'minute 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. The above code will be understood at a glance, so there is no need to explain it more.
Call the function and output directly:
$times="1316932222";
echo tranTime($times);
First we need to understand several functions of time:
time(): Returns the current Unix timestamp
date(): Format a local time/date.
Application example:
Copy the codeThe code is as follows:
date("Y-m-d H:i:s",time()); //Format the current time, output: 2011-9-24 07:27:35
strtotime(): parses the date-time description of any English text into a Unix timestamp.
Application example:
Copy the codeThe code is as follows:
echo strtotime("+1 day"), "\n"; //Output timestamp 1 day ago: 131693222
date_default_timezone_set(): Set the default time zone to use.
Generally, we set the Beijing time: date_default_timezone_set("PRC");
After understanding the above functions, let’s write the timeline function:
The principle of this function is to compare the current time of the system with the target time, obtain a difference, and then compare the difference with the time range (converted into seconds), and output different results according to the range in the time axis (such as: 5 minutes ago). For ease of calculation, we convert all times to Unix timestamps.
Copy the codeThe code is as follows:
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.'minute 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. The above code will be understood at a glance, so there is no need to explain it more.
Call the function and output directly:
Copy the codeThe code is as follows:
$times="1316932222";
echo tranTime($times);