我正在考虑在 Wordpress 帖子上获得“2 天、16 小时、3 分钟等之前发布”等样式的时间戳,这里的 Glavic 代码看起来很有趣。
在 PHP 中将时间戳转换为时间前,例如 1 天前,2 天前...
function time_elapsed_string($datetime, $full = false) {
$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = array(
'y' => 'year',
'm' => 'month',
'w' => 'week',
'd' => 'day',
'h' => 'hour',
'i' => 'minute',
's' => 'second',
);
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
} else {
unset($string[$k]);
}
}
if (!$full) $string = array_slice($string, 0, 1);
return $string ? implode(', ', $string) . ' ago' : 'just now';
}
我需要修改哪些部分来接收 Wordpress 的 the_date 函数(如果这是正确的)和 Unix 时间戳?
谢谢!
您提供的函数需要一个日期字符串。
Wordpress 有一个功能来检索名为的帖子的日期 get_the_date()
您唯一需要做的就是将这两者以一种更简单的方式组合在一起:echo time_elapsed_string(get_the_date(), true)
并将此行放置在发布模板代码中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句