我正在尝试转换一个unix时间戳来显示像Facebook和Twitter.例如,当您在Twitter上看到推文或评论时,您会看到如下所示的日期/时间:
‘2分钟前’或’2天前’或’2周前’
有谁知道任何功能让它像这样工作.我猜这将是一个定制的.
任何帮助深表感谢
如果您使用PHP,您可能想尝试Matt Jones发布的以下功能
原文链接:https://www.f2er.com/php/132357.html// 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'; }