【问题标题】:If statement returning NO, should return YES如果语句返回 NO,则应返回 YES
【发布时间】:2018-06-14 09:07:59
【问题描述】:

我是 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";
}

【问题讨论】:

  • 试试var_dump($today, $first_day_only);你应该明白为什么
  • 我太新手了,无法理解究竟是为什么,但也许是关于将字符串与 int 进行比较?无论哪种方式,这足以让我找到解决方案:
  • 如果你想要一个好的反射,使用一些 php 函数来显示你的 var 的值,它有助于理解真正发生的事情:) var_dump($var); 做这项工作,但你可以使用 echo $var如果 $var 是一个字符串,print_r($var) 也很有用...所以如果某些东西不起作用,请尝试了解您对代码的真正要求;)

标签: php date comparison


【解决方案1】:

解决办法:

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

【讨论】:

    猜你喜欢
    • 2011-11-16
    • 2018-12-03
    • 2020-05-07
    • 2018-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多