Comparing two Dates using strtotime() in PHP

15,009

Solution 1

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.

Solution 2

The format (when used like that) is DD-MM-YYYY. There is no 14th month.

Solution 3

I see you accepted answer but just to make sure you understand. There are 2 cases that dates are parsed.

  1. American - month/day/year
  2. European - day.month.year or day-month-year

You have error because you provided european format "07-14-2013" and there's no 14 month in year.

The proper format for you is one of these:

  • 14-07-2013 - Europe
  • 14.07.2013 - Europe
  • 07/14/2013 - American

Morover, to compare datatime it's better to use object oriented solution and DataTime object. You can read more here.

Share:
15,009
David Folksman
Author by

David Folksman

Updated on June 18, 2022

Comments

  • David Folksman
    David Folksman almost 2 years

    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?

  • David Folksman
    David Folksman almost 11 years
    I have forced the format to $now = date('m-d-Y', strtotime($getCurrentDate . "+0 day")); $four_days_have_passed = date('m-d-Y', strtotime($getRepDate . "+4 days"));
  • zerkms
    zerkms almost 11 years
    @David Folksman: no you haven't. strtotime doesn't expect it to be in american format.
  • David Folksman
    David Folksman almost 11 years
    Hence why I used the format m-d-y, i dont understand your answer?
  • zerkms
    zerkms almost 11 years
    @David Folksman: strtotime accepts the date to be in either YYYY-MM-DD (preferred) or DD-MM-YYYY format. That's it. More accepted formats here: php.net/manual/en/datetime.formats.date.php
  • David Folksman
    David Folksman almost 11 years
    Works here? writecodeonline.com/php date_default_timezone_set('Europe/London'); $getCurrentDate = str_replace('/', '-',$_REQUEST['date']); $four_days_have_passed = date('m-d-Y', strtotime($getCurrentDate . "+4 days")); $now = date('m-d-Y', strtotime($rep1 . "+0 day")); echo $four_days_have_passed; echo "<br>".$now; if ($now < $four_days_have_passed) { echo "<br> booya"; }
  • zerkms
    zerkms almost 11 years
    Well, it's up to you to not trust me and documentation. If you think that it "works there" - use it, good luck :-)
  • Robert
    Robert almost 11 years
    he doesn't need / he needs proper format. He can use 14-07-2013 too :)
  • maxhb
    maxhb over 6 years
    Welcome to SO. To make your answer really helpful you should add a small example.