php – 将unix时间戳转换为twitter / facebook风格

前端之家收集整理的这篇文章主要介绍了php – 将unix时间戳转换为twitter / facebook风格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试转换一个unix时间戳来显示像Facebook和Twitter.例如,当您在Twitter上看到推文或评论时,您会看到如下所示的日期/时间:

‘2分钟前’或’2天前’或’2周前’

有谁知道任何功能让它像这样工作.我猜这将是一个定制的.

任何帮助深表感谢

如果您使用PHP,您可能想尝试Matt Jones发布的以下功能

http://www.mdj.us/web-development/php-programming/another-variation-on-the-time-ago-php-function-use-mysqls-datetime-field-type/

// DISPLAYS COMMENT POST TIME AS "1 year,1 week ago" or "5 minutes,7 seconds ago",etc...
function time_ago($date,$granularity=2) {
    $date = strtotime($date);
    $difference = time() - $date;
    $periods = array('decade' => 315360000,'year' => 31536000,'month' => 2628000,'week' => 604800,'day' => 86400,'hour' => 3600,'minute' => 60,'second' => 1);

    foreach ($periods as $key => $value) {
        if ($difference >= $value) {
            $time = floor($difference/$value);
            $difference %= $value;
            $retval .= ($retval ? ' ' : '').$time.' ';
            $retval .= (($time > 1) ? $key.'s' : $key);
            $granularity--;
        }
        if ($granularity == '0') { break; }
    }
    return ' posted '.$retval.' ago';      
}
原文链接:https://www.f2er.com/php/132357.html

猜你在找的PHP相关文章