Generally, Unix timestamps are used to store time in mysql database. So what if it is more friendly when we display the time? Today I will share with you 3 encapsulated formatting functions. Friends in need can refer to it. Next
Generally, articles with a relatively new display time in CMS need to be displayed a few minutes ago, a few days ago, but generally the database records are timestamps (at least I am used to this), so a conversion is needed In the process, I encapsulated two pieces of code based on the information on the Internet and my own modifications
The following is the encapsulated method
function formatTime($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; }
Or more detailed
function formatTime($time) { $text = ''; $time = intval($time); $ctime = time(); $t = $ctime - $time; //时间差 if ($t < 0) { return date('Y-m-d', $time); } ; $y = date('Y', $ctime) - date('Y', $time);//是否跨年 switch ($t) { case $t == 0: $text = '刚刚'; break; case $t < 60://一分钟内 $text = $t . '秒前'; break; case $t < 3600://一小时内 $text = floor($t / 60) . '分钟前'; break; case $t < 86400://一天内 $text = floor($t / 3600) . '小时前'; // 一天内 break; case $t < 2592000://30天内 if($time > strtotime(date('Ymd',strtotime("-1 day")))) { $text = '昨天'; } elseif($time > strtotime(date('Ymd',strtotime("-2 days")))) { $text = '前天'; } else { $text = floor($t / 86400) . '天前'; } break; case $t < 31536000 && $y == 0://一年内 不跨年 $m = date('m', $ctime) - date('m', $time) -1; if($m == 0) { $text = floor($t / 86400) . '天前'; } else { $text = $m . '个月前'; } break; case $t < 31536000 && $y > 0://一年内 跨年 $text = (11 - date('m', $time) + date('m', $ctime)) . '个月前'; break; default: $text = (date('Y', $ctime) - date('Y', $time)) . '年前'; break; } return $text; }
Share a similar
//格式化友好显示时间 function formatTime($time){ $now=time(); $day=date('Y-m-d',$time); $today=date('Y-m-d'); $dayArr=explode('-',$day); $todayArr=explode('-',$today); //距离的天数,这种方法超过30天则不一定准确,但是30天内是准确的,因为一个月可能是30天也可能是31天 $days=($todayArr[0]-$dayArr[0])*365+(($todayArr[1]-$dayArr[1])*30)+($todayArr[2]-$dayArr[2]); //距离的秒数 $secs=$now-$time; if($todayArr[0]-$dayArr[0]>0 && $days>3){//跨年且超过3天 return date('Y-m-d',$time); }else{ if($days<1){//今天 if($secs<60)return $secs.'秒前'; elseif($secs<3600)return floor($secs/60)."分钟前"; else return floor($secs/3600)."小时前"; }else if($days<2){//昨天 $hour=date('h',$time); return "昨天".$hour.'点'; }elseif($days<3){//前天 $hour=date('h',$time); return "前天".$hour.'点'; }else{//三天前 return date('m月d号',$time); } } }
The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
php format timestamp displays friendly time, php formatting
thinkPHP formatting time problem
The above is the detailed content of Detailed explanation of php formatted timestamp graphic and text. For more information, please follow other related articles on the PHP Chinese website!