PHP Facebook风格时间以前

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Facebook风格时间以前相关的知识,希望对你有一定的参考价值。

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }
    
    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");
    
    $now             = time();
    $unix_date         = strtotime($date);
    
       // check validity of date
    if(empty($unix_date)) {    
        return "Bad date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";
        
    } else {
        $difference     = $unix_date - $now;
        $tense         = "from now";
    }
    
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    
    $difference = round($difference);
    
    if($difference != 1) {
        $periods[$j].= "s";
    }
    
    return "$difference $periods[$j] {$tense}";
}

$date = "2009-03-04 17:45";
$result = nicetime($date); // 2 days ago

?>

以上是关于PHP Facebook风格时间以前的主要内容,如果未能解决你的问题,请参考以下文章

暂时移动 UINavigationView - Facebook 风格

如何进行 facebook 风格转换

Phonegap - 向右滑动 Facebook 风格的导航/布局

如何使用 UICollectionview 播放 Facebook 风格的视频

如何使用浏览器重定向创建 Facebook 风格的应用内身份验证?

忘记以前登录的 facebook 用户?