当前位置:Gxlcms > PHP教程 > 相对时间函数

相对时间函数

时间:2021-07-01 10:21:17 帮助过:2人阅读

<无详细内容>
  1. function relativeTime($time = false, $limit = 86400, $format = 'g:i A M jS') {
  2. if (empty($time) || (!is_string($time) && !is_numeric($time))) $time = time();
  3. elseif (is_string($time)) $time = strtotime($time);
  4. $now = time();
  5. $relative = '';
  6. if ($time === $now) $relative = 'now';
  7. elseif ($time > $now) $relative = 'in the future';
  8. else {
  9. $diff = $now - $time;
  10. if ($diff >= $limit) $relative = date($format, $time);
  11. elseif ($diff < 60) {
  12. $relative = 'less than one minute ago';
  13. } elseif (($minutes = ceil($diff/60)) < 60) {
  14. $relative = $minutes.' minute'.(((int)$minutes === 1) ? '' : 's').' ago';
  15. } else {
  16. $hours = ceil($diff/3600);
  17. $relative = 'about '.$hours.' hour'.(((int)$hours === 1) ? '' : 's').' ago';
  18. }
  19. }
  20. return $relative;
  21. }

人气教程排行