Reputation: 2661
I've got a jquery datepicker
$(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,
});
});
with some validation
$this->validate($request, [
"date" => 'required|date',
]);
and some conversion for the date to pass through to the database
$date = $request->input('date');
$date2 = DateTime::createFromFormat('m/d/Y',$date);
$date3 = $date2->format("Y-d-m");
If I dd($date3) at this point, with a chosen date, I get something like this
"2016-29-09"
All well and good, mysql appears to save in a y-d-m format.
Now when I go to pass it through
Message::where('id', $messageId)->update([
'updated_at' => Carbon\Carbon::now(),
'subject' => 'Status: Price quote delivered.',
'quoted' => 1,
'price' => $request->input('price'),
'estimated_date' => $date3,
]);
everything gets passed to the database except the date. The date just stays at 0000-00-00 in the database.
Help?
Upvotes: 1
Views: 2439
Reputation: 1916
I see that you are using a javascript date object, then it is validated in Laravel, why don't you use Carbon? If the date is validated, then instead of reinventing the wheel, just let Carbon and Laravel handle all the date work:
$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,
]);
Upvotes: 1
Reputation: 16117
Note that you are saving date as YEAR-DAY-MONTH
and the correct format for DATE
is YEAR-MONTH-DAY
.
Also note that value of $date3
is "2016-29-09", this will insert value in your table as "0000-00-00" due to invalid value of Month and correct range of Month is (1-12) for data type DATE
.
Solution:
You need to change the date format (YEAR-MONTH-DAY) as per your Data type of column.
$date3 = $date2->format("Y-m-d"); // something like 2016-09-26
You can also follow the reference: http://dev.mysql.com/doc/refman/5.7/en/datetime.html
Upvotes: 3
Reputation: 1578
All you have to do with this:
From:
$date3 = $date2->format("Y-d-m");
To:
$date3 = $date2->format("Y-m-d");
Upvotes: 2
Reputation: 26258
Bro, check this line:
All well and good, mysql appears to save in a y-d-m format. Now when I go to pass it through
Mysql default format is
Y-m-d
not
y-d-m
Change the format and try again.
Upvotes: 2