首页 > 解决方案 > 如果语句返回 NO,则应返回 YES

问题描述

我是 PHP 新手,试图比较 IF 语句中的两个日期。我的问题是 IF 语句在应该返回 YES 时返回 NO。

$current_date = date('m-d-Y');
$today = strtotime($current_date);
$first_day_only = new DateTime('first day of this month');

//If today is greater than the first day of the month, then YES else NO
if($today > $first_day_only){
    echo "YES";
} else {
    echo "NO";
}

标签: phpdatecomparison

解决方案


解决方案:

 $today = new DateTime();
    $first_day_only = new DateTime('first day of this month');

    //If today is greater than the first day of the month, then YES else NO
    if($today->format('Y-m-d') > $first_day_only->format('Y-m-d')){
        echo "YES";
    } else {
        echo "NO";
    }

推荐阅读