我需要在类型为 datetime
的 SQL 字段中介绍我的日期和时间。目前,日期的格式是mm/dd/yyyy
,我的时间格式hh:ii
。我尝试将此数据发送为
$datetime=$date.' '.$time
,但没有工作,无论输入是什么,在数据库中我总是有00-00-00 00:00:00:00
.
我该如何解决这个问题?
<?php
$date="01/01/2017"; // This is the format you mentioned
$time="22:11"; // This is the format you mentioned
$time=strtotime("$date $time"); //if strtotime() confuses mm with dd, use mktime()
echo date("Y-m-d H:i:s",$time); // MySQL Format
?>
您可以将其发送为 yyyy-mm-dd hh:km。数据库将始终正确选取该选项。如果您只有日期作为字符串,则需要使用 strtotime 或 substr 对其进行转换。
mysql 日期时间的默认格式是 YYYY-MM-DD HH:II::SS因此,要将自定义日期格式插入mysql,您需要将列的数据类型指定为varchar
或者,您可以根据mysql的默认格式将日期存储到表中,出于显示目的,您可以使用mysql的功能自定义日期格式DATE_FORMAT