Simon Staton
Simon Staton

Reputation: 4505

PHP Checking Date Past

I am using RFC822 date format and trying to get my if statement to work however it will not and I cant work out why, this is what the data is echoing as:

$currentdate = Fri, 01 Mar 13 22:24:02 +0000
$post['created_on'] = Sat, 17 Nov 2012 19:26:46 +0100

This is my statement:

$currentdate = date(DATE_RFC822, strtotime("-7 days"));
if ($post['created_on'] < $currentdate) 
{
  echo "test";
}
else
{

}

I am trying to check if the array created on is within the last 7 days, I assume its something to do with the "<" in the statement or the way the date is formated?

Thanks, Simon

Upvotes: 0

Views: 121

Answers (2)

hek2mgl
hek2mgl

Reputation: 157967

You code cannot work as you doing an alphanumerical comparison. RFC822 is not designed for that.

Note that Fri ... is lower than Sat ... is such comparison as F comes before S in alphabet.

Use the DateTime class:

$currentdate = new DateTime('-7days +0100'); // ! use the same tz offset as the post !
$postdate = new DateTime('Sat, 17 Nov 2012 19:26:46 +0100');

if($postdate < $currentdate) {
  // ... do stufff
}

Upvotes: 0

landons
landons

Reputation: 9547

You want to compare timestamps:

<?php
if (strtotime($post['created_on']) >= strtotime('-7 days'))
{
    // Created in the last seven days
}

Upvotes: 1

Related Questions