<?php /** * 类似微信的时间显示 * 规则是:今天的,显示几秒前,几分钟前,几小时前,昨天的显示昨天 上午 XX:XX * 再往前,本周的,显示周几+时间,再往前,本年的,显示月日+时间,再往前,显示年月日+时间 * User: tu6ge * Date: 2016/7/21 * Time: 15:21 */ function timespan($time){ $the_day_time = strtotime(date(‘Y-m-d‘,$time)); $one_day_second = $time-$the_day_time; if($one_day_second<3600*6){ $pre_name = ‘凌晨‘; }elseif($one_day_second<3600*12){ $pre_name = ‘上午‘; }elseif($one_day_second<3600*18){ $pre_name = ‘下午‘; }else{ $pre_name = ‘晚上‘; } $now_time = time(); $today_time = strtotime(date(‘Y-m-d‘)); $yesterday_time = $today_time-86400; $now_week = date(‘w‘,$now_time); $now_week==0 && $now_week=7; $now_week_time = $today_time-($now_week-1)*86400; $now_year_time = strtotime(date(‘Y-01-01‘)); $the_week = date(‘w‘,$time); $the_week==0 && $the_week=7; $week_pre_name = ‘‘; switch ($the_week){ case 1: $week_pre_name = ‘一‘; break; case 2: $week_pre_name = ‘二‘; break; case 3: $week_pre_name = ‘三‘; break; case 4: $week_pre_name = ‘四‘; break; case 5: $week_pre_name = ‘五‘; break; case 6: $week_pre_name = ‘六‘; break; case 7: $week_pre_name = ‘日‘; break; default: $week_pre_name = ‘‘; } $test = ‘‘; if($time>=$now_time){ //未来 $test = ‘‘; }elseif($time>$today_time){ //今天 $diff_time = $now_time-$time; if($diff_time<60){ $test = $diff_time.‘秒前‘; }elseif($diff_time<3600){ $test = intval($diff_time/60).‘分钟前‘; }else{ $test = intval($diff_time/3600).‘小时前‘; } }elseif($time>$yesterday_time){ //昨天 $test = ‘昨天 ‘.$pre_name.‘ ‘.date(‘H:i‘,$time); }elseif($time>$now_week_time){ //这周内 $test = ‘周‘.$week_pre_name.‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time); }elseif($time>$now_year_time){ //今年 $test = date(‘m月d日‘,$time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time); }else{ //更早 $test = date(‘Y月m月d日‘,$time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$time); } return $test; } $time = time()-2; for($i=0;$i<30;$i++){ echo timespan($time); echo ‘<br>‘; $time = $time-3600*6; } //########################上面是用函数实现的 //########################下面是用对象实现的 define(‘NOW_TIME‘,time()); define(‘TODAY_TIME‘,strtotime(date(‘Y-m-d‘,NOW_TIME))); define(‘YESTERDAY_TIME‘,TODAY_TIME-86400); $now_week = date(‘w‘,NOW_TIME); $now_week==0 && $now_week=7; define(‘NOW_WEEK_TIME‘,TODAY_TIME-($now_week-1)*86400); define(‘NOW_YEAR_TIME‘,strtotime(date(‘Y-01-01‘))); class timespan{ public $time; //要计算的时间的时间戳 const now_time=NOW_TIME; const today_time=TODAY_TIME; const yesterday_time = YESTERDAY_TIME; const now_week_time = NOW_WEEK_TIME; const now_year_time = NOW_YEAR_TIME; function __construct($time=null) { !empty($time) && $this->time = $time; $this->time = $time; } /** * 返回 上午,下午,晚上,凌晨 * @param null $time * @return string */ function get_pre_name($time=null){ !empty($time) && $this->time = $time; $the_day_time = strtotime(date(‘Y-m-d‘,$this->time)); $one_day_second = $this->time-$the_day_time; if($one_day_second<3600*6){ $pre_name = ‘凌晨‘; }elseif($one_day_second<3600*12){ $pre_name = ‘上午‘; }elseif($one_day_second<3600*18){ $pre_name = ‘下午‘; }else{ $pre_name = ‘晚上‘; } return $pre_name; } function get_week_name($time=null){ !empty($time) && $this->time = $time; $the_week = date(‘w‘,$this->time); $the_week==0 && $the_week=7; $week_pre_name = ‘‘; switch ($the_week){ case 1: $week_pre_name = ‘一‘; break; case 2: $week_pre_name = ‘二‘; break; case 3: $week_pre_name = ‘三‘; break; case 4: $week_pre_name = ‘四‘; break; case 5: $week_pre_name = ‘五‘; break; case 6: $week_pre_name = ‘六‘; break; case 7: $week_pre_name = ‘日‘; break; default: $week_pre_name = ‘‘; } return $week_pre_name; } function formate($time = null){ !empty($time) && $this->time = $time; $test = ‘‘; if($this->time>=self::now_time){ //未来 $test = ‘‘; }elseif($this->time>self::today_time){ //今天 $diff_time = self::now_time-$this->time; if($diff_time<60){ $test = $diff_time.‘秒前‘; }elseif($diff_time<3600){ $test = intval($diff_time/60).‘分钟前‘; }else{ $test = intval($diff_time/3600).‘小时前‘; } }elseif($this->time>self::yesterday_time){ //昨天 $pre_name = $this->get_pre_name(); $test = ‘昨天 ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time); }elseif($this->time>self::now_week_time){ //这周内 $pre_name = $this->get_pre_name(); $week_pre_name = $this->get_week_name(); $test = ‘周‘.$week_pre_name.‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time); }elseif($this->time>self::now_year_time){ //今年 $pre_name = $this->get_pre_name(); $test = date(‘m月d日‘,$this->time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time); }else{ //更早 $pre_name = $this->get_pre_name(); $test = date(‘Y月m月d日‘,$this->time).‘ ‘.$pre_name.‘ ‘.date(‘H:i‘,$this->time); } return $test; } } //调用方法 $obj = new timespan(); for($i=0;$i<30;$i++){ echo $obj->formate($time); echo ‘<br>‘; $time = $time-3600*6; }
时间: 2024-10-02 00:08:35