企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# php友好时间输出 ***** >[info]代码 ~~~ /** * 友好时间显示,支持过云时和未来时 * @param $timestamp int 时间戳 * @return null|string */ function dateFormat($timestamp) { if ($timestamp > time()) { $formats = array( 'DAY' => '还有%s天', 'DAY_HOUR' => '还有%s天%s小时', 'HOUR' => '还有%s小时', 'HOUR_MINUTE' => '还有%s小时%s分', 'MINUTE' => '还有%s分钟', 'MINUTE_SECOND' => '还有%s分钟%s秒', 'SECOND' => '还有%s秒', ); $seconds = $timestamp - time(); } else { $formats = array( 'DAY' => '%s天前', 'DAY_HOUR' => '%s天%s小时前', 'HOUR' => '%s小时', 'HOUR_MINUTE' => '%s小时%s分前', 'MINUTE' => '%s分钟前', 'MINUTE_SECOND' => '%s分钟%s秒前', 'SECOND' => '%s秒前', ); $seconds = time() - $timestamp; } /* 计算出时间差 */ $minutes = floor($seconds / 60); $hours = floor($minutes / 60); $days = floor($hours / 24); if ($days > 0) { $diffFormat = 'DAY'; } else { $diffFormat = ($hours > 0) ? 'HOUR' : 'MINUTE'; if ($diffFormat == 'HOUR') { $diffFormat .= ($minutes > 0 && ($minutes - $hours * 60) > 0) ? '_MINUTE' : ''; } else { $diffFormat = (($seconds - $minutes * 60) > 0 && $minutes > 0) ? $diffFormat . '_SECOND' : 'SECOND'; } } $dateDiff = null; switch ($diffFormat) { case 'DAY': $dateDiff = sprintf($formats[$diffFormat], $days); break; case 'DAY_HOUR': $dateDiff = sprintf($formats[$diffFormat], $days, $hours - $days * 60); break; case 'HOUR': $dateDiff = sprintf($formats[$diffFormat], $hours); break; case 'HOUR_MINUTE': $dateDiff = sprintf($formats[$diffFormat], $hours, $minutes - $hours * 60); break; case 'MINUTE': $dateDiff = sprintf($formats[$diffFormat], $minutes); break; case 'MINUTE_SECOND': $dateDiff = sprintf($formats[$diffFormat], $minutes, $seconds - $minutes * 60); break; case 'SECOND': $dateDiff = sprintf($formats[$diffFormat], $seconds); break; } return $dateDiff; } ~~~