繁体   English   中英

判断MySQL返回的datetime字符串是否为空的最佳方法?

[英]Best way to tell if datetime string returned from MySQL is empty?

未在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这样的is_date但我能找到的是一个名为checkdate的函数,它似乎不是我想要的。

我想我可以使用:

if(strtotime($date) > 0)

我没有看到任何明显的问题,但我仍然认为可能有更好的东西。

标准方式是什么?

$dbDate = '0000-00-00 00:00:00';

if (strtotime($dbDate) == 0){
  echo 'Empty date';
}

http://www.ideone.com/eNffC

if ((int)$dbDate) echo 'Date is set';

对于基于unix epoch转换的解决方案,可能存在一些与时区相关的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM