【发布时间】: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