Heim  >  Artikel  >  Backend-Entwicklung  >  php判断之前的时间段

php判断之前的时间段

高洛峰
高洛峰Original
2016-10-21 10:28:461144Durchsuche

<?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;
}
?>


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:PHP字符串截取长度自定义方法Nächster Artikel:PHP验证日常格式