php实现求相对时间函数

yipeiwu_com5年前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的JSON封装、转变及输出操作示例

本文实例讲述了PHP的JSON封装、转变及输出操作。分享给大家供大家参考,具体如下: Json封装 protected function renderJSON($data=[], $...

解析func_num_args与func_get_args函数的使用

func_num_args函数功能– 返回传递到函数的参数数目,其语法如下 : int func_num_args (void )。说明 : 返回传递到目前定义函数的参数数目。如果是从函...

PHP-CGI进程CPU 100% 与 file_get_contents 函数的关系分析

后来,我通过跟踪发现,这类情况的出现,跟 PHP 的 file_get_contents() 函数有着密切的关系。   大、中型网站中,基于 HTTP 协议的 API 接口调用,是家常便...

php基础知识:类与对象(3) 构造函数和析构函数

构造函数 PHP 5 允行开发者在一个类中定义一个方法作为构造函数。具有构造函数的类会在每次创建对象时先调用此方法,所以非常适合在使用对象之前做一些初始化工作。&nb...

PHP字符串与数组处理函数用法小结

本文实例讲述了PHP字符串与数组处理函数用法。分享给大家供大家参考,具体如下:字符串处理函数trim --去除字符串首尾的多余空白字符和其他字符函数结构:string trim&...