Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


PHP if condition with date...

I'm trying to figure out how to get PHP to tell if "dateA" is newer than "dateB". The date format is a little different than the normal 2014-03-21.

Here is my date format: - 30 Sep, 2011 12:00pm

$dateA = '21 Mar, 2014 12:00pm';
$dateB = '30 Sep, 2011 12:00pm';                

if(strtotime($dateA) > strtotime($dateB))
    echo "dateA is newer";
else {
    echo "dateB is newer";

How can I get this to work? Any help is appreciated!

~ Timothy

7 Answers


Would you be able to strip the comma before the conditional statement?


$dateA = '21 Mar, 2014 12:00pm';
$dateB = '30 Sep, 2011 12:00pm';

$dateA = str_replace(',', '', $dateA);
$dateB = str_replace(',', '', $dateB);

if(strtotime($dateA) > strtotime($dateB))
        echo "dateA is newer";
    } else {
        echo "dateB is newer";

Jeff, thanks! that works perfect :)

$dateA = strtotime('21-03-2014'); $dateB = strtotime('30-09-2011'); This converts them to the number of seconds since January 1, 1970, so your comparison should work. not sure if it would work with $dateA = '21 Mar, 2014 12:00pm'; $dateB = '30 Sep, 2011 12:00pm';

Hi Timothy,

I ran the code on my computer and it echoed "dateB is older".


Thanks for the reply Jeff,

Yes, but if you look at the code I'm asking if "dateA" is greater-than "dateB" which at that moment it is. The code seems not to care and just prints out dateB is newer every time.

... I think it might be the date format issue but I need to use that date format.

How do I get php to understand that date format and to know when the date is a newer date?

I do believe you would have to compare the individual times to another time (such as the current date) to genuinely tell which is more current? Basically setting a reference point for them to be compared off of.

Or try changing your date syntax to

2014-03-21 12:00:00.0

Thanks Ricardo,

That does work and i've already tried it :) but I need to use the date format I asked.

For example - "21 Mar, 2014 12:00pm"

never done php but this might work

if(strtotime($dateA) >= strtotime(12, 0, 0, 9, 30, 2011)) { echo "It works!"; } else { echo "it doesn't"; }

Hi John, Interesting... i'm messing with that now. Seems like something like that would work, right?

Right now your script is giving me an error: Warning: strtotime() expects at most 2 parameters, 6 given in Untitled-1.php on line 7


Timothy Logue try the date syntax I provided, it should work well with this.

@Ricardo Hill-Henry - thanks that does work but I have to use the date format "21 Mar, 2014 12:00pm". I'm using a third party software that records the date format like that.


Can you lose the comma after the month?

nope :(