【发布时间】:2018-03-30 09:53:59
【问题描述】:
如下所示,我尝试在event 表中插入类似2018-03-31 的日期,但没有成功。输入类型 date 的日期结果。它是我数据库中的DateTime,但知道时间对我没有用。那么,为什么是DateTime?因为如果用户不提供日期,我想要当前日期,而 DATE 数据类型我不能。
您能帮我解决这个问题并了解如何正确地将日期插入 MySQL 表。
$sql = "INSERT INTO event (titre,content,image,date) VALUES (?,?,?,?)";
$connexion = Connect::getConnexion();
try{
$query = $connexion->prepare($sql);
$d = date('Y-m-d',strtotime($date));
$query->bindParam(1,$titre,PDO::PARAM_STR,100);
$query->bindParam(2,$content,PDO::PARAM_STR,8000);
$query->bindParam(3,$d,PDO::PARAM_STR);
$query->bindParam(4,$image,PDO::PARAM_STR,8000);
$connexion->beginTransaction();
$query->execute();
$lastInsert = $connexion->lastInsertId();
$connexion->commit();
}
【问题讨论】:
-
将
" 00:00:00"附加到日期的格式部分。date将按原样解析它。 -
我不明白为什么当用户不提供日期时你不能使用
DATE类型。无论哪种方式,您都应该输入该列所期望的内容,因此要么更改它,要么正确输入它。 -
$d = date('Y-m-d 00:00:00',strtotime($date));不工作。对于我的数据库中的 DATETIME 格式,当我在我的 sql 管理面板中声明 DATE 类型并且我在默认值中选择“CURRENT_TIMESTAMP”时出现错误“默认值无效”。
-
当 $date 为 "2018-03-31" 时,$d 为 "1970-01-01 00:00:00" 且 $d = date('Y-m-d 00:00:00', strtotime($date));
-
在mysql的表结构中只能选择DATE