简体   繁体   English

如何修改此PHP函数以接收Wordpress帖子的日期?

[英]How do I modify this PHP function to receive the date of Wordpress posts?

I'm looking at getting a "posted 2 days, 16 hours, 3 minutes etc ago" etc style of timestamp on Wordpress posts, and Glavic's code here looked interesting. 我正在寻找在Wordpress帖子上获得“ 2天,16小时,3分钟等时间发布”等时间戳的样式,而Glavic的代码看起来很有趣。

Converting timestamp to time ago in PHP eg 1 day ago, 2 days ago... 在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'; 
}

What parts do I modify to receive Wordpress's the_date function (if that's the correct one) and a Unix timestamp? 我要修改哪些部分以接收Wordpress的the_date函数(如果正确的话)和Unix时间戳?

Thanks! 谢谢!

The function you have provided expects a date string. 您提供的函数需要一个日期字符串。

Wordpress has a function to retrieve the date of a post called get_the_date() Wordpress具有检索名为get_the_date()的帖子的日期的功能。

Only thing you need is to actually combine those two together in a more less this way: echo time_elapsed_string(get_the_date(), true) and place this line within post template code. 您唯一需要做的就是将这两者实际上以一种更少的方式组合在一起: echo time_elapsed_string(get_the_date(), true)并将此行放置在发布模板代码中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM