Home >Backend Development >PHP Tutorial >relative time function

relative time function

WBOY
WBOYOriginal
2016-07-25 09:09:241053browse
  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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php filter functionNext article:php filter function