未在MySQL中设置的日期时间字段将返回 0000-00-00 00:00:00
确定日期是否未在PHP中设置的最佳方法是什么?
没有,如果这些工作:
if($date) //will return true because it is a valid string
if(empty($date)) //will also return true.
if(strtotime($date)) //I thought this would work but it returns a negative number and is true.
我可以使用:
if($date == '0000-00-00 00:00:00')
但是,如果我之后将列类型更改为日期,则会中断。所以我也不喜欢这个。
我正在寻找像is_date
这样的东西,但我能找到的是一个名为 checkdate 的功能,似乎不是我想要的。
我想我可以使用:
if(strtotime($date) > 0)
我没有看到任何明显的问题,但我仍然认为可能有更好的东西。
标准方式是什么?
答案 0 :(得分:5)
$dbDate = '0000-00-00 00:00:00';
if (strtotime($dbDate) == 0){
echo 'Empty date';
}
答案 1 :(得分:1)
if ((int)$dbDate) echo 'Date is set';
对于基于unix epoch转换的解决方案,可能存在一些与时区相关的问题。