加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php实现求相对时间函数

发布时间:2020-12-13 02:40:28 所属栏目:PHP教程 来源:网络整理
导读:《:php实现求相对时间函数》要点: 本文介绍了:php实现求相对时间函数,希望对您有用。如果有疑问,可以联系我们。 本篇章节讲解php实现求相对时间函数.供大家参考研究.具体实现办法如下: PHP学习 ?phpfunction relativeTime($time = false,$limi

《:php实现求相对时间函数》要点:
本文介绍了:php实现求相对时间函数,希望对您有用。如果有疑问,可以联系我们。

本篇章节讲解php实现求相对时间函数.分享给大家供大家参考.具体实现办法如下:PHP学习

<?php
function relativeTime($time = false,$limit = 86400,$format = 'g:i A M jS') {
  if (empty($time) || (!is_string($time) & amp; & amp;
  !is_numeric($time))) $time = time();
  elseif (is_string($time)) $time = strtotime($time);
  $now = time();
  $relative = '';
  if ($time === $now) $relative = 'now';
  elseif ($time > $now) $relative = 'in the future';
  else {
    $diff = $now - $time;
    if ($diff >= $limit) $relative = date($format,$time);
    elseif ($diff < 60) {
      $relative = 'less than one minute ago';
    } elseif (($minutes = ceil($diff / 60)) < 60) {
      $relative = $minutes . ' minute' . (((int)$minutes === 1) ? '' : 's') . ' ago';
    } else {
      $hours = ceil($diff / 3600);
      $relative = 'about ' . $hours . ' hour' . (((int)$hours === 1) ? '' : 's') . ' ago';
    }
  }
  return $relative;
}

希望本文所述对大家的php程序设计有所赞助.

欢迎参与《:php实现求相对时间函数》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读