Php 未使用laravel将日期保存到mySQL数据库
我有一个jquery日期选择器Php 未使用laravel将日期保存到mySQL数据库,php,mysql,laravel,date,Php,Mysql,Laravel,Date,我有一个jquery日期选择器 $(function() { var date = new Date(); var currentMonth = date.getMonth(); var currentDate = date.getDate(); var currentYear = date.getFullYear(); $('.datepicker').datepicker({ showOn: 'focus',changeMonth: true,
$(function() {
var date = new Date();
var currentMonth = date.getMonth();
var currentDate = date.getDate();
var currentYear = date.getFullYear();
$('.datepicker').datepicker({
showOn: 'focus',changeMonth: true,
minDate: new Date(currentYear, currentMonth, currentDate),
changeYear: true,
});
});
经过一些验证
$this->validate($request, [
"date" => 'required|date',
]);
并对日期进行一些转换以传递到数据库
$date = $request->input('date');
$date2 = DateTime::createFromFormat('m/d/Y',$date);
$date3 = $date2->format("Y-d-m");
如果我在这一点上添加$date3,选择一个日期,我会得到这样的结果
"2016-29-09"
很好,mysql似乎以y-d-m格式保存
现在当我要通过它的时候
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date3,
]);
除了日期之外,所有内容都会传递到数据库。数据库中的日期仅为0000-00-00
帮助?兄弟,检查这行:
很好,mysql似乎以y-d-m格式保存。
现在当我要通过它的时候
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date3,
]);
Mysql默认格式为
Y-m-d
不是
y-d-m
更改格式,然后重试。兄弟,检查此行:
很好,mysql似乎以y-d-m格式保存。
现在当我要通过它的时候
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date3,
]);
Mysql默认格式为
Y-m-d
不是
y-d-m
更改格式,然后重试。您需要做的就是: 发件人: $date3=$date2->formatY-d-m 致:
$date3=$date2->formatY-m-d 你要做的就是: 发件人: $date3=$date2->formatY-d-m 致:
$date3=$date2->formatY-m-d 请注意,您正在将日期保存为年月日,正确的日期格式为年月日 还请注意,$date3的值是2016-29-09,这将在表中插入值0000-00-00,因为月份的值无效,数据类型DATE的正确月份范围为1-12 解决方案: 您需要根据列的数据类型更改日期格式YEAR-MONTH-DAY
$date3 = $date2->format("Y-m-d"); // something like 2016-09-26
您也可以按照参考资料进行操作:
请注意,您正在将日期保存为年月日,正确的日期格式为年月日 还请注意,$date3的值是2016-29-09,这将在表中插入值0000-00-00,因为月份的值无效,数据类型DATE的正确月份范围为1-12 解决方案: 您需要根据列的数据类型更改日期格式YEAR-MONTH-DAY
$date3 = $date2->format("Y-m-d"); // something like 2016-09-26
您也可以按照参考资料进行操作:
我看到您使用的是javascript日期对象,然后它在Laravel中得到验证,为什么不使用Carbon?如果日期得到验证,那么就让Carbon和Laravel处理所有的日期工作,而不是重新发明轮子:
$date = new \Carbon\Carbon($request->input('date'));
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date,
]);
我看到您使用的是javascript日期对象,然后它在Laravel中得到验证,为什么不使用Carbon?如果日期得到验证,那么就让Carbon和Laravel处理所有的日期工作,而不是重新发明轮子:
$date = new \Carbon\Carbon($request->input('date'));
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date,
]);
使用硬编码值'estimated_date'=>'2016-09-26',而不是2016-26-09进行测试您必须通过Y-m-d格式的日期。$date3=$date2->formatY-m-d;如果您的问题已经解决,则选择最佳答案并标记为已接受,这将使用硬编码值'estimated_date'=>'2016-09-26',而不是2016-26-09,您必须使用Y-m-d格式传递日期。$date3=$date2->formatY-m-d;如果您的问题已经解决,那么选择最佳答案并将其标记为已接受,这将向其他人说明这到底是为什么在查看数据库时会出现类似2016-09-28的条目??哦,好吧。我根本不在乎。现在可以了。ThanksBro,2016-09-28是Y-m-d格式,28不是月份。所以,只要睁大眼睛,无论我是疯了还是我需要睡觉,那为什么在查看数据库时,条目会像2016-09-28那样出现呢??哦,好吧。我根本不在乎。现在可以了。ThanksBro,2016-09-28是Y-m-d格式,28不是月份。所以睁大眼睛,不管我是疯了还是我需要睡觉请检查它还在那里请检查它还在那里