Page 1 of 1

Comparing 2 dates using strtotime()

Posted: Thu Oct 27, 2011 9:45 am
by grabber_grabbs
Man, do i have hard time with this one.... this is probably simple you will say.

I need to compare the received date of a product to see if this is a new product or not... simple ennough !

$purchdate = $db_field['DATEDEBUT'];// here DATEDEBUT is 2011-09-20
$strtoday = strtotime("now");

$isnewitem = strtotime($purchdate + 15);
if ($isnewitem >= $strtoday) {
$newstock = true;
} else {
$newstock = false;
}

here i want the product that are 15 days old or less to be selected as newstock. Looks like the +15 is not working here.

thanks.

Re: Comparing 2 dates using strtotime()

Posted: Thu Oct 27, 2011 12:20 pm
by manohoo
dates are tricky.
Let's convert everything to seconds:

Code: Select all

$purchdate = '2011-09-20';
$purchdate = strtotime($purchdate); // purchdate in seconds
$today = strtotime(date('Y-m-d')); // today in seconds
$diff = ($today - $purchdate)/(60*60*24); // converted to days
echo $diff;
That's it, I am sure you can take from here.