PHP友好的时间显示

完全逻辑代码

1/** 2 * 友好的时间展示 3 * @param int $time 时间戳 4 * @param string $format 默认显示格式化方式 5 * @return string 返回时间,错误时返回为空字符串 6 */ 7function diy_friend_date($time, $format='Y-m-d') 8{ 9 if (!$time) 10 return ''; 11 12 $nowtime = time(); 13 if ($time > $nowtime){ 14 return date($format, $time); 15 } 16 17 $Y = date('Y', $time);//年份 18 $z = date('z', $time);//当前的第几天 19 $nowY = date('Y', $nowtime); 20 $nowz = date('z', $nowtime); 21 22 if ($z > $nowz){ 23 $nowz += 365; 24 } 25 $diffz = $nowz - $z;//获取差异天 26 $diffs = $nowtime - $time;//获取差异秒 27 28 if ($diffz >= 365){ 29 return ($nowY-$Y).'年前'; 30 } elseif ($diffz >= 30){ 31 return floor($diffz / 30).'个月前'; 32 } elseif ($diffz >= 7){ 33 return floor($diffz / 7).'个星期前'; 34 } elseif ($diffz >= 3){ 35 return $diffz.'天前'; 36 } elseif ($diffz >= 2){ 37 return '前天'; 38 } elseif ($diffz >= 1) { 39 return '昨天'; 40 } elseif ($diffs >= 3600) { 41 return floor($diffs / 3600).'小时前'; 42 } elseif ($diffs >= 300) { 43 return floor($diffs / 60).'分钟前'; 44 } else {//五分钟内 45 return '刚刚'.$diffs.'秒'; 46 } 47}

频繁更新的网站,这样可能效率不高,可以二分或者三分,减少if判断次数,自己根据情况判断

1/** 2 * 友好的时间展示 3 * @param int $time 时间戳 4 * @param string $format 默认显示格式化方式 5 * @return string 返回时间,错误时返回为空字符串 6 */ 7function diy_friend_date($time, $format='Y-m-d') 8{ 9 if (!$time) 10 return ''; 11 12 $nowtime = time(); 13 if ($time > $nowtime){ 14 return date($format, $time); 15 } 16 17 $Y = date('Y', $time);//年份 18 $z = date('z', $time);//当前的第几天 19 $nowY = date('Y', $nowtime); 20 $nowz = date('z', $nowtime); 21 22 if ($z > $nowz){ 23 $nowz += 365; 24 } 25 $diffz = $nowz - $z;//获取差异天 26 $diffs = $nowtime - $time;//获取差异秒 27 28 if ($diffz <= 7){ 29 if ($diffz >= 3){ 30 return $diffz.'天前'; 31 } elseif ($diffz >= 2){ 32 return '前天'; 33 } elseif ($diffz >= 1) { 34 return '昨天'; 35 } elseif ($diffs >= 3600) { 36 return floor($diffs / 3600).'小时前'; 37 } elseif ($diffs >= 300) { 38 return floor($diffs / 60).'分钟前'; 39 } else {//五分钟内 40 return '刚刚'.$diffs.'秒'; 41 } 42 } else { 43 if ($diffz >= 365){ 44 return ($nowY-$Y).'年前'; 45 } elseif ($diffz >= 30){ 46 return floor($diffz / 30).'个月前'; 47 //} elseif ($diffz >= 7){ 48 } else{ 49 return floor($diffz / 7).'个星期前'; 50 } 51 } 52 53}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

​一篇文章总结一下Python库中关于时间的常见操作

前言本次来总结一下关于Python时间的相关操作,有一个有趣的问题。如果你的业务用不到时间相关的操作,你的业务基本上会一直用不到。但是如果你的业务一旦用到了时间操作,你就会发现,淦,到处都是时间操作。。。所以思来想去,还是总结一下吧,本次会采用类型注解方式。time包importtime时间戳从1970年1月1日00:00:00标准时区诞生到现在

Django之Django模板

1、问:html页面从数据库中读出DateTimeField字段时,显示的时间格式和数据库中存放的格式不一致,比如数据库字段内容为2012082616:00:00,但是页面显示的却是Aug.26,2012,4p.m.答:为了页面和数据库中显示一致,需要在页面格式化时间,需要添加<td{{dayrecord.p\_time|date: