【问题标题】:subtract 6 hours from wordpress get_the_time [duplicate]从wordpress get_the_time中减去6小时[重复]
【发布时间】:2017-10-02 08:18:31
【问题描述】:

我正在尝试从 wordpress 函数 get_the_time() 中减去一定的小时数;所以帖子看起来像是以前发布过的。

这是我的代码:

<?php  function displaytime() {
return get_the_time(strtotime( '-6 hours' ) );
}
        echo displaytime(); ?>

问题是现在的输出是:1506910342

有人可以帮帮我吗?

提前谢谢你

【问题讨论】:

    标签: php wordpress subtraction


    【解决方案1】:

    正如您在docs 中看到的,get_the_time 函数的第一个参数是日期格式,它以您指定的格式返回字符串。因此,您可以使用 U 作为第一个参数,这样您将获得 UNIX 时间,然后使用 strtotime 减去 6 小时,然后使用 date 函数再次格式化:

     date('Y-m-d H:i:s', strtotime('-6 hours', (int)get_the_time('U')));
    

    【讨论】:

    • 已解决,非常感谢您的帮助,我使用的代码是:&lt;?php $date = date('Y-m-d H:i:s', strtotime('-6 hours', (int)get_the_time('U'))); echo $date; ?&gt;
    【解决方案2】:

    1506910342 是一个 unix 时间戳。

    您可以简单地使用 this 方法从 unix 时间戳中获取日期时间

    例如:

    $ts = get_the_time(); 
    $ts -= 6 * 60 * 60;
    $date = new DateTime();
    $date->setTimestamp($ts);
    

    【讨论】:

    • 谢谢你的回答,我现在设置的时间是正确的,但是它并没有从时间中减去6个小时,这里是代码''
    • 您需要从时间戳中减去秒数,而不是“-6 小时”。对于六个小时,这将是 21600。您也可以使用 6 * 60 * 60
    • 抱歉,我是 php 新手,所以我必须设置 '-21600' 而不是 '-6 hours' ?
    • 在您的问题中,您有以下语句 return get_the_time(strtotime( '-6 hours' ) );你说它会给你一个时间戳。您现在只需从时间戳中减去 21600。看到我的答案,我已经编辑了!
    • 例如...? :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多