【问题标题】:Get the time of published post in wordpress [duplicate]在wordpress中获取发布帖子的时间[重复]
【发布时间】:2017-08-01 16:30:33
【问题描述】:

我的网站上有一个博客。在博客列表中,我想显示日期和时间。我用the_time() 获得发布的发布日期。但是获取时间是一个棘手的部分。

我不想显示发布时间,例如1:30 PM

我想显示帖子的持续时间......我的意思是这样

53 minutes ago 要么 4 hours ago 要么 2 days ago 要么 5 months ago 要么 1 year ago

我首先尝试此代码 get_post_time('U', $post->ID)get_post_time('U', true) 但它给了我时间戳,就像这样 1486128075

也可以试试这个get_the_time(),但它给了我1:00 PM

希望你能理解我的问题

【问题讨论】:

  • 为什么不自己计算呢?
  • 尝试this并使用get_the_time()函数。

标签: php wordpress datetime timestamp


【解决方案1】:

将此代码添加到您的functions.php中

function ash_relative_time() { 
$post_date = get_the_time('U');
$delta = time() - $post_date;
if ( $delta < 60 ) {
    echo 'Just Now';
}
elseif ($delta > 60 && $delta < 120){
    echo '1 minute ago';
}
elseif ($delta > 120 && $delta < (60*60)){
    echo strval(round(($delta/60),0)), ' minutes ago';
}
elseif ($delta > (60*60) && $delta < (120*60)){
    echo 'About an hour ago';
}
elseif ($delta > (120*60) && $delta < (24*60*60)){
    echo strval(round(($delta/3600),0)), ' hours ago';
}
else {
    echo the_time('j\<\s\u\p\>S\<\/\s\u\p\> M y g:i a');
}}

然后使用echo ash_relative_time();打印出来。

【讨论】:

    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多