Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing two Dates using strtotime() in PHP

I have two variables containing strings of dates in the format

$four_days_have_passed = "07-14-2013";
$now = "07-10-2013";

I have checked the output in FirePHP and the dates are correct.

Then I try to compare them like this,

if (strtotime($now) < strtotime($four_days_have_passed))
{
  Do Stuff
}

Why does the code inside the IF statement never execute?

like image 495
David Folksman Avatar asked Dec 26 '22 00:12

David Folksman


1 Answers

If you want to use MM/DD/YYYY format you need / separator.

$four_days_have_passed = "07/14/2013";
$now = "07/10/2013";

From the manual:-

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

like image 195
Daniel Kmak Avatar answered Jan 04 '23 23:01

Daniel Kmak