user2997779
user2997779

Reputation: 397

Check if the post_date was one minute ago PHP

I fetched into an array the wp_posts table in wordpress. I want to make a cron job that would check if the post_date of each entry was one minute ago.

I tried this, but it doesn`t work.

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);
}

How should I do it?

Upvotes: 1

Views: 482

Answers (3)

ins0
ins0

Reputation: 3928

just add to the post 60 seconds and check if the timestamp is higher then the current time. if the time is higher the post was posted within 1 minute.

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

Upvotes: 1

BenM
BenM

Reputation: 53228

Well, assuming that $postDate contains what you think it does, the following code should be sufficient:

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

}

Please note that this checks if the post date is exactly one minute ago. To check for one minute or older, use $postDate <= (time() - 60).

Upvotes: 2

Cl&#233;ment Malet
Cl&#233;ment Malet

Reputation: 5090

Why would you need a $formatedDate ?

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

if ($postDate < $currentDate - 60) {

}

Upvotes: 1

Related Questions