如果用户没有输入日期,MySQL 会插入 0000-00-00 00:00:00。 现在我不想在文本框中显示日期,如果它是 0000-00-00 00:00:00
我在 codeigniter 中使用这个助手
function dateTimeHelp($date, $mysqlDatetime = true) {
if (strtotime($date) === false)
return null;
else {
if ($mysqlDatetime) {
return date('Y-m-d H:i:s', strtotime($date));
} else {
return date('d-m-Y H:i:s', strtotime($date));
}
}
}
多种方式,我假设调用此函数以在您的表单中显示。只需检查
0000-00-00 00:00:00
:
if ($date == '0000-00-00 00:00:00' || strtotime($date) === false)
return null;
如果将 MySQL 列更改为非空并将默认值添加为当前时间戳会怎样?
created_at DATETIME NOT NULL 默认 CURRENT_TIMESTAMP,
使用此功能意味着不需要代码,并且提交表单时,它将使用放入数据库的日期和时间
if ($date == '0000-00-00 00:00:00' || strtotime($date) === false) 返回空值;