是的,您可以使用PHP实现这一点。
这是a function 来自@arnorhs:
$time = strtotime(\'2010-04-28 17:25:43\');
echo \'event happened \'.humanTiming($time).\' ago\';
function humanTiming ($time)
{
$time = time() - $time; // to get the time since that moment
$time = ($time<1)? 1 : $time;
$tokens = array (
31536000 => \'year\',
2592000 => \'month\',
604800 => \'week\',
86400 => \'day\',
3600 => \'hour\',
60 => \'minute\',
1 => \'second\'
);
foreach ($tokens as $unit => $text) {
if ($time < $unit) continue;
$numberOfUnits = floor($time / $unit);
return $numberOfUnits.\' \'.$text.(($numberOfUnits>1)?\'s\':\'\');
}
}
WordPress的方式实际上有很多。但WordPress自1.5.0版以来就有一个内置功能,这对于WordPress插件的开发非常方便:
<?php human_time_diff( $from, $to ); ?>
从现在开始显示注释的运行时间很简单,如下所示:
echo human_time_diff( get_comment_time( \'U\' ), current_time( \'timestamp\' ) ) .\' ago\';
参考号:ul
human_time_diff()
- WordPress法典