简体   繁体   English

MySQL时间戳插入可以,但有时会失败

[英]MySQL Timestamp INSERT okay but sometimes fails

So I've got a bootstrap 3 form and there are a clockpicker and a datepicker plugins in it. 所以我有一个bootstrap 3形式,并且有一个clockpickerdatepicker在它的插件。 I get all the data through PHP and try to insert into MySQL (currently in XAMPP). 我通过PHP获取所有数据,并尝试插入MySQL (当前在XAMPP中)。 Sometimes the INSERT is okay, but sometimes one or both of the Timestamp fields in the DB are left at 0000.... 有时INSERT可以,但是有时DB中的一个或两个Timestamp字段保留为0000。

Since I display the date like 17/09/2016 but I try to save it like 2016-17-09 in MySQL and I have to concatenate the text from the clockpicker to the text from the date picker, I do that: 由于我将日期显示为17/09/2016但我尝试将日期保存为2016-17-09 ,因此我必须将时钟选择器中的文本连接到日期选择器中的文本,所以我这样做:

    $startDate = $_POST['shiftStartDate'] . " " . $_POST['shiftStartTime'];
$startDateFormat = str_replace('/', '-', $startDate);
$date1 = date_create($startDateFormat);
$dateTime1 = date_format($date1, 'Y-d-m H:i:s');



$endDate = $_POST['shiftEndDate'] . " " . $_POST['shiftEndTime'];
$endDateFormat = str_replace('/', '-', $endDate);
$date2 = date_create($endDateFormat);
$dateTime2 = date_format($date2, 'Y-d-m H:i:s');

Any ideas? 有任何想法吗?

SOLUTION : When formatting, conform to MySQL's requirement of Ymd, NOT Ydm: 解决方案 :格式化时,请遵守MySQL的Ymd要求,而不是Ydm:

$dateTime2 = date_format($date2, 'Y-m-d H:i:s');

MYSQL expects the date format to be YYYY-MM-DD HH:MM:SS when you save it to the database in a DATETIME or TIMESTAMP column datatype 当您将日期格式保存为DATETIME或TIMESTAMP列数据类型到数据库时,MYSQL期望日期格式为YYYY-MM-DD HH:MM:SS

You are trying to save it as YYYY-DD-MM. 您正在尝试将其另存为YYYY-DD-MM。 That will only work if the DD is less than 12 so that probably explains why it sometimes works and sometimes does not 仅当DD小于12时才起作用,这可能可以解释为什么它有时起作用而有时不起作用

So format your dates correctly to what MYSQL expects and all will be well. 因此,将日期正确格式化为MYSQL期望的格式,一切都会很好。

I would also expect that you would be getting some odd results when you read back the dates that did save to the database. 我还希望当您读回确实保存到数据库的日期时会得到一些奇怪的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM