如何过滤日期 0000-00-00 00:00:00 如果用户没有在我的表单中输入

问题描述 投票:0回答:3

如果用户没有输入日期,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));
    }
}

}
php mysql date strtotime
3个回答
1
投票

多种方式,我假设调用此函数以在您的表单中显示。只需检查

0000-00-00 00:00:00

if ($date == '0000-00-00 00:00:00' || strtotime($date) === false)
    return null;

1
投票

如果将 MySQL 列更改为非空并将默认值添加为当前时间戳会怎样?

created_at DATETIME NOT NULL 默认 CURRENT_TIMESTAMP,

使用此功能意味着不需要代码,并且提交表单时,它将使用放入数据库的日期和时间


0
投票

if ($date == '0000-00-00 00:00:00' || strtotime($date) === false) 返回空值;

© www.soinside.com 2019 - 2024. All rights reserved.