header("Content-type:text/html;charset=utf-8");
function relative_date($date) {
if (!preg_match('/^\d+$/', $date)) $date = strtotime(trim($date));
$sec = time() - $date;
switch(true){
case $sec < 60:
return 60 - $sec. '秒前';
case $sec < 3600:
return round($sec/60). '分钟前';
case $sec < 86400:
return round($sec/3600). '小时前';
case $sec < (86400 * 7):
return round($sec/86400). '天前';
case $sec < (86400 * 7 * 4):
return round($sec/(86400*7)). '周前';
default:
return date("Y年n月d日",$date);
}
}
$date = '2014-11-20 13:36:12';
echo relative_date($date);
php实现时间显示方式(2秒前)
最新推荐文章于 2020-09-27 13:55:08 发布