php实现求相对时间函数

yipeiwu_com6年前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 adodb连接mssql解决乱码问题

周海汉/文ADO可以用 new COM("ADODB.Connection", NULL, CP_UTF8)//65001 这样的语句来实现正确转换。但ADO对php的支持缺乏文档。而有...

PHP中几种常见的超时处理全面总结

在PHP开发中工作里非常多使用到超时处理到超时的场合,我说几个场景: 1. 异步获取数据如果某个后端数据源获取不成功则跳过,不影响整个页面展现 2. 为了保证Web服务器不会因为当个页面...

thinkphp Apache配置重启Apache1 restart 出错解决办法

概要:   thinkphp 在Apache上配置启用伪静态,重启Apache1 restart 竟然失败了,报错     Job for httpd.service failed be...

php+html5基于websocket实现聊天室的方法

本文实例讲述了php+html5基于websocket实现聊天室的方法。分享给大家供大家参考。具体如下: html5的websocket 实现了双向通信,折腾了几天弄了个聊天室,分享给大...

php使用Jpgraph绘制饼状图的方法

php使用Jpgraph绘制饼状图的方法

本文实例讲述了php使用Jpgraph绘制饼状图的方法。分享给大家供大家参考。具体实现方法如下: <?php include ("src/jpgraph.php");...