Comparing 2 dates using strtotime()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
grabber_grabbs
Forum Commoner
Posts: 60
Joined: Mon Oct 10, 2011 6:13 pm

Comparing 2 dates using strtotime()

Post 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.
User avatar
manohoo
Forum Contributor
Posts: 201
Joined: Wed Dec 23, 2009 12:28 pm

Re: Comparing 2 dates using strtotime()

Post 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.
Post Reply