This article is a detailed analysis of PHPCodeBase: shows the time as just n minutes ago. For more information, see
This article is a detailed analysis of PHP CodeBase: shows the time as just n minutes/hour ago. For more information, see
In many cases, in order to display the timeliness of information, the time is usually displayed as "just", "5 minutes ago", "3 hours ago", and so on, instead of printing the time directly. For Weibo, SNS applications use this function for the longest time. Generally, the time format stored in the database is Unix Timestamp and server space. Therefore, a PHP function that converts the Unix timestamp into a timeline display is recorded here.
The function is relatively simple. You can understand the Code directly.
The Code is as follows:
Date_default_timezone_set ('prc ');
USD 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 = 'gang ';
}
Elseif ($ time <60*60)
{
$ Min = floor ($ time/60 );
$ Str = $ min. 'minute ago ';
}
Elseif ($ time <60*60*24)
{
$ H = floor ($ time/(60*60 ));
$ Str = $ h. 'hour before '. $ htime;
}
Elseif ($ time <60*60*24*3)
{
$ D = floor ($ time/(60*60*24 ));
If ($ d = 1)
$ Str = 'Yesterday '. $ rtime;
Else
$ Str = 'day before Day'. $ rtime;
}
Else
{
$ Str = $ rtime;
}
Return $ str;
}
?>
Note that the $ time parameter in the transfer_time () function must be a Unix timestamp in the U.S. space. If not, convert it to a Unix timestamp using strtotime ().
, Server space