php Laravel:将日期保存到 MySQL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29658978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:30:29  来源:igfitidea点击:

Laravel: saving date to MySQL

phpmysqldatetimelaravel-4

提问by jbehrens94

In my controller, when I createan event, it saves perfectly fine. The user enters a date in dd-mm-yyyyand it gets saved in MySQL DATETIMEformat. Then the detailsview renders it completely fine, just like the editview via model binding. As soon as I try to save from the editform, the date somehow fails and returns 1970-01-01 00:00:00.

在我的控制器中,当我create发生事件时,它保存得很好。用户输入一个日期dd-mm-yyyy,它会以 MySQLDATETIME格式保存。然后details视图将它完全渲染,就像edit通过模型绑定的视图一样。一旦我尝试从edit表单中保存,日期就会以某种方式失败并返回1970-01-01 00:00:00.

I am not really sure why this only happens on my updatemethod, as my storemethod is in essence the same.

我不确定为什么这只发生在我的update方法上,因为我的store方法本质上是一样的。

$input = Input::all();
$input['plannedTime'] = date('Y-m-d H:i:s', strtotime(Input::get('plannedTime')));

How comes the updatemethod returns no error, validation is alright, but it still won't save it correctly?

为什么update方法没有返回错误,验证没问题,但它仍然无法正确保存?

回答by ceejayoz

The value of 'plannedTime' is a string of date format d-m-Y H:i:s.

'plannedTime' 的值是一个日期格式为 dmY H:i:s 的字符串。

There's your problem. PHP's strtotimedoes its best, but 04-05-2015 00:00:00could be either April 5 or May 4, depending where you live.

那是你的问题。PHPstrtotime会尽力而为,但04-05-2015 00:00:00可能是 4 月 5 日或 5 月 4 日,具体取决于您住在哪里。

As an example, on my install, strtotime('04-13-2014 00:00:00')fails (which'll get converted to 0, which'll get converted to 1970-01-01).

例如,在我的安装中,strtotime('04-13-2014 00:00:00')失败(将转换为 0,将转换为 1970-01-01)。

Laravel's datevalidation expects a value that strtotime can handle. If you're using an ambiguous date format, use createFromFormatto parse it, then formatto spit it out in a more standard format.

Laravel 的date验证需要一个 strtotime 可以处理的值。如果您使用的是不明确的日期格式,请使用createFromFormat解析它,然后format以更标准的格式将其吐出。

$date = DateTime::createFromFormat('d-m-Y H:i:s', Input::get('plannedTime'));
$usableDate = $date->format('Y-m-d H:i:s');

回答by Mathieu Dumoulin

MySQL never returns any error for invalid dates as far as i can remember, it just sets it to EPOCH and thats it!

据我所知,MySQL 永远不会为无效日期返回任何错误,它只是将其设置为 EPOCH,仅此而已!

You should enforce your dates to be always converted to a "Y-m-d H:i:s" format when communicating with your site and display the date in "d-m-Y" only on the output side.

在与您的站点通信时,您应该强制您的日期始终转换为“Ymd H:i:s”格式,并仅在输出端以“dmY”格式显示日期。

You should try and use

你应该尝试使用

public static DateTime DateTime::createFromFormat ( string $format , string $time [, DateTimeZone $timezone ] )

To create a datetime object from a format. If you know your format such as in this case, then provide the format and get a date object back which will be persisted correctly!

从格式创建日期时间对象。如果您知道您的格式,例如在这种情况下,请提供格式并返回一个日期对象,该对象将被正确持久化!