Home > Backend Development > PHP Tutorial > PHP determines the previous time period

PHP determines the previous time period

高洛峰
Release: 2016-10-21 10:28:46
Original
1191 people have browsed it

<?php
/**
 *
 +--------------------------------------------------------------------
 * Description 友好显示时间
 +--------------------------------------------------------------------
 * @param int $time 要格式化的时间戳 默认为当前时间
 +--------------------------------------------------------------------
 * @return string $text 格式化后的时间戳
 +--------------------------------------------------------------------
 * @author yijianqing
 +--------------------------------------------------------------------
 */
function mdate($time = NULL) {
   $text = &#39;&#39;;
    $time = $time === NULL || $time > time() ? time() : intval($time);
    $t = time() - $time; //时间差 (秒)
    if ($t == 0)
        $text = &#39;刚刚&#39;;
    elseif ($t < 60)
        $text = $t . &#39;秒前&#39;; // 一分钟内
    elseif ($t < 60 * 60)
        $text = floor($t / 60) . &#39;分钟前&#39;; //一小时内
    elseif ($t < 60 * 60 * 24)
        $text = floor($t / (60 * 60)) . &#39;小时前&#39;; // 一天内
   elseif ($t < 60 * 60 * 24 * 3)
        $text = floor($time/(60*60*24)) ==1 ? &#39;昨天 &#39; . date(&#39;H:i&#39;, $time) : &#39;前天 &#39; . date(&#39;H:i&#39;, $time) ; //昨天和前天
    elseif ($t < 60 * 60 * 24 * 30)
        $text = date(&#39;m月d日 H:i&#39;, $time); //一个月内
    elseif ($t < 60 * 60 * 24 * 365)
        $text = date(&#39;m月d日&#39;, $time); //一年内
    else
        $text = date(&#39;Y年m月d日&#39;, $time); //一年以前
    return $text;
}
?>
Copy after login


Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template