Php 判断MySQL返回的datetime字符串是否为空的最佳方法?
MySQL中未设置的datetime字段将返回0000-00-00:00:00 确定日期是否未在PHP中设置的最佳方法是什么 如果这些功能正常,则无:Php 判断MySQL返回的datetime字符串是否为空的最佳方法?,php,date,Php,Date,MySQL中未设置的datetime字段将返回0000-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
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')
if(strtotime($date) > 0)
但是,如果我将列类型更改为justDate,它就会中断。所以我也不喜欢这个
我一直在寻找类似于is_date
的东西,但我能找到的只是一个名为checkdate的函数,它似乎不是我想要的
我想我可以用:
if($date == '0000-00-00 00:00:00')
if(strtotime($date) > 0)
我看不出这有什么明显的问题,但我仍然认为可能有更好的办法
标准方式是什么
$dbDate = '0000-00-00 00:00:00';
if (strtotime($dbDate) == 0){
echo 'Empty date';
}
对于基于unix epoch transformations的解决方案,可能存在一些与时区相关的问题。能否更改db字段的默认值?您可以在字段定义中使用默认空值。如果datetime字段仅设置了时间部分,例如
0000-00-00 12:34:56
,则不太可靠。不太可能,但仍然有可能。0000-00-00 12:34:56对我来说似乎不是有效日期。这不起作用,它返回一个负数。请看这里:@JohnIsaacks:必须是时区设置,我得到一个错误的值。测试是否(!strotime($dbDate)){}
将确认一个无效值。