检查post_date是否是一分钟前的PHP


Check if the post_date was one minute ago PHP

我在wordpress中获取wp_posts表的array。我想做一个cron作业,检查每个条目的post_date是否在一分钟前。

我试过了,但是行不通。

foreach($arrayOfPosts as $post){
    $postDate = strtotime($post['post_date']);
    $formatedDate = strtotime($oneMinuteAgo);
    $currentDate= strtotime(date("Y-m-d H:i:s"));
    echo $postDate . "----" . $formatedDate . " <br/>";
    if ($formatedDate <= $postDate){
        array_push($arrayOfNewPosts,$post);
}

我该怎么做?

好吧,假设$postDate包含您认为它的功能,下面的代码应该是足够的:

$postDate = strtotime($post['post_date']);
if($postDate == (time() - 60)
{
}

请注意,这检查发布日期是否恰好是一分钟前。要检查一分钟或更久,请使用$postDate <= (time() - 60)

为什么需要$formatedDate ?

$postDate = strtotime($post['post_date']);
$currentDate= strtotime(date("Y-m-d H:i:s"));
if ($postDate < $currentDate - 60) {
}

只需添加到post 60 seconds并检查时间戳是否高于当前时间。如果时间较长,则帖子在1分钟内发布。

$postDate = strtotime($post['post_date']);
if( ($postDate + 60)  >= time() )
{
    echo "within 1 minute";
}