In many occasions in order to show the timeliness of information, the general will show the time "just", "5 minutes Ago", "3 hours ago," and so on, rather than directly to the time to print out. For example microblogging, SNS class application is the longest use this function. The time format typically stored in the database is the UNIX timestamp, so a PHP function that converts the UNIX timestamp to the timeline display is recorded here.
The function is relatively simple, look directly at the code is very understood.
Copy Code code as follows:
<?php
Date_default_timezone_set (' PRC ');
$date = "1351836000";
echo Trantime ($date);
function Transfer_time ($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. ' Hour ago '. $htime;
}
ElseIf ($time < 60 * 60 * 24 * 3)
{
$d = Floor ($time/(60*60*24));
if ($d ==1)
$str = ' Yesterday '. $rtime;
Else
$str = ' Day before yesterday '. $rtime;
}
Else
{
$str = $rtime;
}
return $str;
}
?>
Note the parameter $time in the function transfer_time () must be a Unix timestamp, and if not, use Strtotime () to convert it to a UNIX timestamp