企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
~~~ /** * 传入日期格式或时间戳格式时间,返回与当前时间的差距,如1分钟前,2小时前,5月前,3年前等 * @param string or int $date 分两种日期格式"2015-09-12 14:16:12"或时间戳格式"1386743303" * @param int $type * @return string */ function format_time($date = 0, $type = 1) { //$type = 1为时间戳格式,$type = 2为date时间格式 switch ($type) { case 1: //$data时间戳格式 $second = time()- $date; $minute = floor($second / 60) ? floor($second / 60) : 1; if ($minute >= 60 && $minute < (60 * 24)) { $hour = floor($minute / 60); } elseif ($minute >= (60 * 24) && $minute < (60 * 24 * 30)) { $day = floor($minute / ( 60 * 24)); } elseif ($minute >= (60 * 24 * 30) && $minute < (60 * 24 * 365)) { $month = floor($minute / (60 * 24 * 30)); } elseif ($minute >= (60 * 24 * 365)) { $year = floor($minute / (60 * 24 * 365)); } break; case 2: //$date为字符串格式 2013-06-06 19:16:12 $date = strtotime($date); $second = time()- $date; $minute = floor($second / 60) ? floor($second / 60) : 1; if ($minute >= 60 && $minute < (60 * 24)) { $hour = floor($minute / 60); } elseif ($minute >= (60 * 24) && $minute < (60 * 24 * 30)) { $day = floor($minute / ( 60 * 24)); } elseif ($minute >= (60 * 24 * 30) && $minute < (60 * 24 * 365)) { $mont = floor($minute / (60 * 24 * 30)); } elseif ($minute >= (60 * 24 * 365)) { $year = floor($minute / (60 * 24 * 365)); } break; default: break; } if (isset($year)) { return $year . '年前'; } elseif (isset($month)) { return $month . '月前'; } elseif (isset($day)) { return $day . '天前'; } elseif (isset($hour)) { return $hour . '小时前'; } elseif (isset($minute)) { return $minute . '分钟前'; } } ~~~ ~~~ /** * 传入时间戳,计算距离现在的时间 * * @param number $time 时间戳 * * @return string 返回多少以前 */ function word_time($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 < 1728000) { $str = sprintf('%d天前', floor($int / 86400)); } else { $str = date('Y-m-d H:i:s', $time); } return $str; } ~~~