方法1:
/** * 个性化日期显示 * @static * @access public * @param datetime $times 日期 * @return string 返回大致日期 * @example 示例 ueTime(‘‘) */ function ueTime($times) { if ($times == ‘‘ || $times == 0) { return false; } //完整时间戳 $strtotime = is_int($times) ? $times : strtotime($times); $times_day = date(‘Y-m-d‘, $strtotime); $times_day_strtotime = strtotime($times_day); //今天 $nowdate_str = strtotime(date(‘Y-m-d‘)); //精确的时间间隔(秒) $interval = time() - $strtotime; //今天的 if ($times_day_strtotime == $nowdate_str) { //小于一分钟 if ($interval < 60) { $pct = sprintf("%d秒前", $interval); } //小于1小时 elseif ($interval < 3600) { $pct = sprintf("%d分钟前", ceil($interval / 60)); } else { $pct = sprintf("%d小时前", floor($interval / 3600)); } } //昨天的 elseif ($times_day_strtotime == strtotime(date(‘Y-m-d‘, strtotime(‘-1 days‘)))) { $pct = ‘昨天‘ . date(‘H:i‘, $strtotime); } //前天的 elseif ($times_day_strtotime == strtotime(date(‘Y-m-d‘, strtotime(‘-2 days‘)))) { $pct = ‘前天‘ . date(‘H:i‘, $strtotime); } //一个月以内 elseif ($interval < (3600 * 24 * 30)) { $pct = date(‘m月d日‘, $strtotime); } //一年以内 elseif ($interval < (3600 * 24 * 365)) { $pct = date(‘m月d日‘, $strtotime); } //一年以上 else { $pct = date(‘Y年m月d日‘, $strtotime); } return $pct; }
方法2:
<?php header("Content-type: text/html; charset=utf8"); date_default_timezone_set("Asia/Shanghai"); //设置时区 function time_tran($the_time) { $now_time = date("Y-m-d H:i:s", time()); //echo $now_time; $now_time = strtotime($now_time); $show_time = strtotime($the_time); $dur = $now_time - $show_time; if ($dur < 0) { return $the_time; } else { if ($dur < 60) { return $dur . ‘秒前‘; } else { if ($dur < 3600) { return floor($dur / 60) . ‘分钟前‘; } else { if ($dur < 86400) { return floor($dur / 3600) . ‘小时前‘; } else { if ($dur < 259200) {//3天内 return floor($dur / 86400) . ‘天前‘; } else { return $the_time; } } } } } } echo time_tran("2014-7-8 19:22:01"); ?>
方法3:
function wordTime($time) { $time = (int) substr($time, 0, 10); $int = time() - $time; $str = ‘‘; if ($int <= 2){ $str = sprintf(‘刚刚‘, $int); }elseif ($int < 60){ $str = sprintf(‘%d秒前‘, $int); }elseif ($int < 3600){ $str = sprintf(‘%d分钟前‘, floor($int / 60)); }elseif ($int < 86400){ $str = sprintf(‘%d小时前‘, floor($int / 3600)); }elseif ($int < 2592000){ $str = sprintf(‘%d天前‘, floor($int / 86400)); }else{ $str = date(‘Y-m-d H:i:s‘, $time); } return $str; }
时间: 2024-12-28 10:45:51