0

i need to compare two date from string, my dates: first date: 11-11-19 second date: 11-24-17

so i try to

$firstdate = "11-11-19";
$seconddate = "11-24-17";

if($firstdate < $seconddate)
    {
        echo "firstdate is minor than the secondate";
    }
    else
    {
        echo "seconddate is major than the firstdate";
    }

if i change < or > the if statement should change, but i get always the firsdate... how to do to compare two dates in this forma mm-dd-yy ? Thanks

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110

2 Answers2

-1

You can use strtotime to convert string to unix timestamp, and compare that.

$firstdate = strtotime("11-11-19");
$seconddate = strtotime("11-24-17");

if($firstdate < $seconddate)
    {
        echo "firstdate is minor than the secondate";
    }
    else
    {
        echo "seconddate is major than the firstdate";
    }
Peter
  • 1,742
  • 15
  • 26
-1

You can convert both dates to timestamp:

echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";
Vladimir
  • 1,373
  • 8
  • 12