防止PHP date()默认为12/31/1969 [英] Prevent PHP date() from defaulting to 12/31/1969

查看:98
本文介绍了防止PHP date()默认为12/31/1969的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MySQL数据库与PHP。我使用 DATETIME 字段在数据库中存储我的日期值。



我正在使用这个PHP代码转换输入日期为MySQL的适当格式。

  date(Ymd H:i:s,strtotime($ inputDate)) 

但是,无论何时日期无效,它将以 1969-12-31 19:00:00



有没有办法将此默认为 0000-00- 00 00:00:00

解决方案

只需检测<$ c $的输出的有效性c> strtotime(),其中返回错误失败



如下所示:

  $ time = strtotime($ inputDate); 
$ date =($ time === false)? '0000-00-00 00:00:00':date('Y-m-d H:i:s',$ time);


I am using a MySQL database with PHP. I store my date values in the database using the DATETIME field.

I'm using this PHP code to convert inputted dates to the appropriate format for MySQL.

date("Y-m-d H:i:s", strtotime($inputDate))

However, whenever a date is invalid, it is placed in the database as 1969-12-31 19:00:00

Is there a way to default this to 0000-00-00 00:00:00 ?

解决方案

Just detect validity with the output of strtotime(), which returns false on failure.

Something like:

$time = strtotime($inputDate);
$date = ($time === false) ? '0000-00-00 00:00:00' : date('Y-m-d H:i:s', $time);

这篇关于防止PHP date()默认为12/31/1969的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆