This article introduces that PHP judges based on time to display the code just now, a few minutes ago, and a few hours ago. Friends in need can refer to it.
Judge the time difference between the release time of the information and the present, and convert it into "just", "a few minutes ago", "a few hours ago", "yesterday", and "the day before yesterday". The code is as follows: <?php /** * php时间转换 * edit by bbs.it-home.org */ date_default_timezone_set('PRC'); $date = "1351836000"; echo tranTime($date); function tranTime($time) { $rtime = date("m-d H:i",$time); $htime = date("H:i",$time); $time = time() - $time; if ($time < 60) { $str = '刚刚'; } elseif ($time < 60 * 60) { $min = floor($time/60); $str = $min.'分钟前'; } elseif ($time < 60 * 60 * 24) { $h = floor($time/(60*60)); $str = $h.'小时前 '.$htime; } elseif ($time < 60 * 60 * 24 * 3) { $d = floor($time/(60*60*24)); if($d==1) $str = '昨天 '.$rtime; else $str = '前天 '.$rtime; } else { $str = $rtime; } return $str; } ?> Copy after login >>>> Articles you may be interested in: PHP implements timeline function (just now, 5 minutes ago) php sample code to get time and how many minutes ago Small example of PHP deleting all files created N minutes ago php implements the function code that posted the information a few minutes ago |