将php中的日期字符串与mm-dd-yy格式进行比较

时间:2018-01-23 16:08:56

标签: php date datetime compare

我需要比较字符串中的两个日期, 我的日期: 第一次约会:11-11-19 第二次约会:11-24-17

所以我试着

$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";
    }

如果我改变&lt;或者&gt; if语句应该改变,但我总是得到第一个... 怎么做比较这个形式mm-dd-yy中的两个日期? 感谢

2 个答案:

答案 0 :(得分:-1)

您可以使用strtotime将字符串转换为unix时间戳,并将其进行比较。

$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";
    }

答案 1 :(得分:-1)

您可以将这两个日期转换为时间戳:

echo (strtotime($firstdate) < strtotime($seconddate)) ? "firstdate is minor than the secondate" : "seconddate is major than the firstdate";
相关问题