gpt4 book ai didi

php - 判断从 MySQL 返回的日期时间字符串是否为空的最佳方法?

转载 作者:可可西里 更新时间:2023-10-31 22:47:18 26 4
gpt4 key购买 nike

未在 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)

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

什么是标准方式?

最佳答案

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

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

http://www.ideone.com/eNffC

关于php - 判断从 MySQL 返回的日期时间字符串是否为空的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890953/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com