博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php时间区间,优化显示
阅读量:6162 次
发布时间:2019-06-21

本文共 4587 字,大约阅读时间需要 15 分钟。

=$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 '
'; $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 '
'; $time = $time-3600*6;}

 对于技术来说,写这个东西,真没技术含量,只所以贴出了,是想表明我想做产品的态度

转载于:https://www.cnblogs.com/tu6ge/p/5694800.html

你可能感兴趣的文章