-2

I'm trying to compare between two date but unfortunately this isn't working by converting this into UNIX format with strtotime. I'm trying to compare a date to another date.

However this format is working:

if(strtotime("22-04-17") < strtotime("25-05-17")){
    echo 'Date One is smaller than date two';
}

But Many times it's failing. I've seen a lot of examples on the web but I can't figure out anything good!

if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the 
    // bigger on but not working
    echo 'Date One is smaller than date two';
}
nerdlyist
  • 2,842
  • 2
  • 20
  • 32
Code Cooker
  • 881
  • 14
  • 19
  • Possible duplicate of [Comparing two dates](https://stackoverflow.com/questions/3847736/comparing-two-dates) – GrumpyCrouton Aug 07 '17 at 14:49
  • 3
    default format for `strtotime` is `Y-m-d` – u_mulder Aug 07 '17 at 14:51
  • @GrumpyCrouton That is actually very complex to understand. I'm just simply trying to compare between two date. So please give me a easier example. – Code Cooker Aug 07 '17 at 14:52
  • 3
    _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._ -- From [strtotime](http://php.net/manual/en/function.strtotime.php) – Milan Chheda Aug 07 '17 at 14:52
  • @AbraCadaver The first one example is working. But the second one isn't working. but still the second is also greater. – Code Cooker Aug 07 '17 at 14:59
  • 1
    Use 4 digit years to disambiguate or use `YY-mm-dd` – AbraCadaver Aug 07 '17 at 15:00
  • 2nd is not greater because on this comparison, you are basically testing if year 22 is less than year 04. – Amit Joshi Aug 07 '17 at 15:05

6 Answers6

3

From the manual (make special note of the part I've put in bold):

"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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d."

So here's what you're doing, with the dates PHP is interpreting your strings as in comments:

// Is 17 April 2022 earlier than 17 May 2025? Yes.
if(strtotime("22-04-17") < strtotime("25-05-17")){
    echo 'Date One is smaller than date two';
}

// Is 17 April 2022 earlier than 17 May 2004? No.
if(strtotime("22-04-17") < strtotime("04-05-17")){ //passing still the 
    // bigger on but not working
    echo 'Date One is smaller than date two';
}

I hope this makes the problem you're having clear.

As it also says in the manual, use DateTime::createFromFormat/date_create_from_format if you want to avoid ambiguity:

 $date = date_create_from_format('d-m-y', '04-05-17'); // 4 May 2017
Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
2

your comparsion is not working because strtotime("22-04-17") actually results to timestamp for this date: 17th April 2022;

Do the following and you will see what I mean. the following code will output '2022-May-17`

$date = "22-05-17";
echo date ("Y-M-d ",strtotime($date))."<br>";
Amit Joshi
  • 1,334
  • 1
  • 8
  • 10
  • I'm not here for negotiation. I want a solution, Please help me if you can – Code Cooker Aug 07 '17 at 15:00
  • 2
    I am not trying to negotiate with you. What I am telling you is why your code is not working. since you have chosen a `-` as separator and your year is in 2 digits, PHP assumes the format as `y-m-d`. The solution here is to use the 4 digit year. – Amit Joshi Aug 07 '17 at 15:07
2

Try this

$date1 = date('d-m-y',strtotime("22-04-17"));
$date2 = date('d-m-y',strtotime("04-05-17"));;

if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the  
    echo 'Date One is smaller than date two';
}

Your year format 17 causing the problem in strtotime function

ßiansor Å. Ålmerol
  • 2,849
  • 3
  • 22
  • 24
  • This one is working !! I don't know if there is a bug. Did you use this on production? – Code Cooker Aug 07 '17 at 15:05
  • @CodeCooker instead of `date('d-m-y',strtotime("22-04-17"));` and `date('d-m-y',strtotime("04-05-17"));` You can just put `strtotime("22-04-17 GMT");` and `strtotime("04-05-17 GMT");` – YaBCK Aug 07 '17 at 15:09
  • This is kinda misleading because if you do `$date1 = date('d-m-Y',strtotime("22-04-17")); $date2 = date('d-m-Y',strtotime("04-05-17")); if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the echo 'Date One is smaller than date two'; }` this fails. – Amit Joshi Aug 07 '17 at 15:15
  • @AmitJoshi Yes It is failing.. ! when I'm using this format `m-d-Y` and giving it like `08-15-17` it is generating 1970 year !! which is incredible. – Code Cooker Aug 07 '17 at 18:17
  • told you :). So to fix this the best thing you can do is use a 4 digit year. where do you get these dates from anyways? May be you can write a function to turn these into proper days. – Amit Joshi Aug 07 '17 at 19:09
0

I know this isn't what you're looking for but have you tried doing this to showing if date greater / smaller?

// Dates
$Date1 = strtotime("22-04-17 GMT");
$Date2 = strtotime("04-05-17 GMT");

// Diff between dates
$Diff = (int)floor(($Date1 - $Date2) / (60*60*24));

if ($Diff < 0) {
   echo "The Diff is negative";
}

Then the other way is like this answer: LINK

$date1 = strtotime("22-04-17 GMT");
$date2 = strtotime("04-05-17 GMT");

if((int)strtotime($date1) < (int)strtotime($date2)){ //passing still the  
    echo 'Date One is smaller than date two';
}
YaBCK
  • 2,949
  • 4
  • 32
  • 61
0

I wrote a function that takes your datestring and properly return timestmp. Please note that I followed PHP's convention of treating 2 digit years, i.e. 00-69 are mapped to 2000-2069 and 70-99 to 1970-1999

/* functon takes dateString of format dd-dd-yy and return proper timestamp */
function getTimestamp($dateString)
{
    $res = preg_match('/^([0-9]{2})\-([0-9]{2})-([0-9]{2})/', $dateString, $matches);
    if(!$res)
        return 0;
    //00-69 are mapped to 2000-2069 and 70-99 to 1970-1999
    if($matches[3]>=70 &&  $matches[3]<=99 )
        $year = "19".$matches[3];
    else
        $year = "20".$matches[3];
    $formatted_dat_string = $year."-".$matches[2]."-".$matches[1];
    return strtotime($formatted_dat_string);
}
getTimestamp("22-04-99");

So you can now use this function instead of strtotime for comparison.

Amit Joshi
  • 1,334
  • 1
  • 8
  • 10
0

Finally, I got the solution. The strtotime() isn't any good to handle this case. You should go for dateTime object instead.

//First you need to pass the original format then you will need to pass new 
//Format to get this working properly. Hope this will help you guy's
$myDateTimestart = DateTime::createFromFormat('d-m-Y', $dateString);
$startDate = $myDateTimestart->format('m-d-Y');
//with Simply that you can format your date properly

I just Messed my times with this thing it was really bad

Code Cooker
  • 881
  • 14
  • 19