Home > Software design >  Why date comparison gives wrong result?
Why date comparison gives wrong result?

Time:02-08

$examDay1 = date("07.02.2022");

$examDay2 = date("18.03.2022");

$examDay3 = date("06.04.2022");

$today = date("d.m.Y");

print_r($examDay1 <= $today);  //true 

print_r($examDay2 <= $today);  // false as I expected

print_r($examDay3 <= $today);  // true for some reason

I am trying to give access to users in same specific dates. For example one of them started today and the comparison worked fine. However, somehow it says $examDay3 <=$today gives true. What am I doing wrong?

CodePudding user response:

The enter image description here

You can use "strtotime" to convert the date string to a timestamp integer, and use it for comparison.

$examDay1 = date("07.02.2022");

$examDay2 = date("18.03.2022");

$examDay3 = date("06.04.2022");

$today = time();

var_dump(
    strtotime($examDay1) <= $today,
    strtotime($examDay2) <= $today,
    strtotime($examDay3) <= $today
);
  •  Tags:  
  • Related