php日期比较返回false

php日期比较返回false,php,datetime,Php,Datetime,请有人让我知道什么是错误的下面的代码 比较不起作用,date_diff函数输出为false $strtime = strtotime("2021-01-01"); $rowdate = date("d-n-Y", $strtime); $nextmonthyear = date("d-n-Y", strtotime('first day of +1 month')); if ($rowdate >

请有人让我知道什么是错误的下面的代码

比较不起作用,date_diff函数输出为false

    $strtime = strtotime("2021-01-01");
    $rowdate = date("d-n-Y", $strtime);
    $nextmonthyear = date("d-n-Y", strtotime('first day of +1 month'));
    if ($rowdate > $nextmonthyear) {
        echo "cont";
    }
如果您显示正在比较的日期

echo $rowdate."->".$nextmonthyear;
你得到

01-1-2021->01-8-2020
但实际上,您将它们作为字符串进行比较,这将得到8,并将其与另一个日期中的1进行比较

您应该使用Y-n-d格式来表示日期,或者最好还是使用日期值本身

$strtime = strtotime("2021-01-01");
$nextmonthyear = strtotime('first day of +1 month');
if ($strtime > $nextmonthyear) {
    echo "cont";
}
我认为您需要执行$strtime>strotime“1个月的第一天”
然后使用date函数将其格式化以显示

date返回一个字符串。如果要将它们作为日期而不是字符串进行比较,请使用“日期”格式,而不是“创建”格式。

我实际上以为我是在比较对象而不是字符串。但是对于一个对象,我们需要新的关键字。。明白了,谢谢