ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
一:时间计算函数 ~~~ 注:考虑到时区后修改如下: function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60); $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; } } } ~~~ 二:时间排序 注:以时间更新来排序 ~~~ usort($data, function($a, $b) { $al = strtotime($a['create_time']); $bl = strtotime($b['create_time']); if ($al == $bl) return 0; return ($al > $bl) ? -1 : 1; }); ~~~ 三:对数组时间字段的值遍历修改 ~~~ for($i=0;$i<6;$i++){ $data[$i]['create_time']=$this->time_tran($data[$i]['create_time']); } return json($data); ~~~ 注意:如果$data包含的数组数量小于6则会报错 四:合并数组 ~~~ $data1=Db::table('sbh_artnews')->where(['type'=>$type,'ad'=>0])->order(['istop','create_time'=>'desc'])->limit(6)->page($page)->field('content',true)->select(); $data2=Db::table('sbh_artnews')->where(['type'=>$type,'ad'=>1])->order('create_time','desc')->limit(2)->page($page)->field('content',true)->select(); $data=array_merge($data1,$data2); ~~~ 五,秒转换成时分秒 ~~~ php /** * 处理时间(将 秒数 转为 时分秒 ) * * @param string $total_time 秒数 * @return string $ftime 处理好的时间 */ function ftime($total_time) { $s = gmdate('H:i:s', $total_time); // dump($s);die; $time = explode(':', $s); $time['0'] == '00' ? $h = 0 : $h = $time['0']; $time['1'] == '00' ? $m = 0 : $m = $time['1']; $time['2'] == '00' ? $s = 0 : $s = $time['2']; $ftime = ( empty($h) ? ( empty($m) ? $s .'秒' : ( empty($s) ? $m . '分' : $m . '分' . $s .'秒' ) ) :( empty($m) && empty($s) ? $h .'小时' : ( empty($m) ? $h . '时' . $s . '秒' : ( empty($s) ? $h . '小时' . $m . '分' : $h . '小时' . $m . '分' .$s . '秒' ) ) ) ); return $ftime; } 时间格式:34:02:02 function changeTimeType($seconds){ if ($seconds>3600){ $hours = intval($seconds/3600); $time = $hours.":".gmstrftime('%M:%S', $seconds); }else{ $time = gmstrftime('%H:%M:%S', $seconds); } return $time; } ~~~