简体   繁体   中英

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

You want to compare timestamps:

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM