当没有选择日期时,1970-01-01被插入数据库 [英] 1970-01-01 is getting inserted to database when no date is selected

查看:87
本文介绍了当没有选择日期时,1970-01-01被插入数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框,在php中输入日期。因为我想要的日期是特定的格式我正在使用。
date(d.m.Y,strtotime())转换日期。当没有日期给出或者如果我将文本框留空,它将输入1970-01-01到数据库。我该如何解决?
这是我的代码

  $ d_date = clean($ _ POST ['datum']); 
$ newdate = date(Y-m-d,strtotime($ d_date));

如果 $ d_date value为null将进入1970-01-01到数据库。

解决方案

使数据库中的字段为空,当日期未以表单形式设置为NULL时。 mysql的默认行为是将日期设置为默认值,即1970-01-01(unix epoch)


I have a text box where date is entered in php. Since I want the date to be in particular format I am using. date("d.m.Y", strtotime()) to conver the date. When no date is give or if I leave the textbox blank it will enter 1970-01-01 to database. How can I solve this? Here is my code

 $d_date= clean($_POST['datum']);
$newdate = date("Y-m-d", strtotime( $d_date));

If $d_date value is null the it will enter 1970-01-01 to database.

解决方案

Make field in the database nullable, and when date is not submitted in the form set it to NULL. Default behaviour of mysql is to set date to default value, which is 1970-01-01(unix epoch)

这篇关于当没有选择日期时,1970-01-01被插入数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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