In many occasions in order to show the timeliness of information, the time is generally displayed as "just", "5 minutes Ago", "3 hours ago", and so on, rather than directly print out the time. such as microblogging, SNS applications for the longest use of this feature. The time format typically stored in the database is a UNIX timestamp, so here is a PHP function that converts the UNIX timestamp to the timeline display.
<?Phpdate_default_timezone_set (' PRC ');$date= "1351836000";EchoTrantime ($date);functionTransfer_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.‘ 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;}?>
Note that the parameter $time in the function transfer_time () must be a UNIX timestamp, and if not, convert it to a UNIX timestamp first with strtotime ().
PHP Time arithmetic