I'm trying to make my Laravel API exchange dates with my Angularjs frontend.
It works from Laravel to JS by first converting my mysql datetime initial value:
2015-08-19 10:00:00
using $newdate = Carbon::parse($event['date'])->toATOMString();
which outputs:
2015-08-19T10:00:00+00:00
, then converting it later to a javascript date object (Angularjs) using event.date = new Date(event.date);
which outputs:
Date 2015-08-19T10:00:00.000Z
Problem: posting my updated Javascript date object back to my PHP API to update the value in mysql db (datetime). Carbon doesn’t like the date format he gets back:
2015-08-19T11:00:00.000Z
And I’m not sure how to handle this. I get the following error from my Laravel log: exception 'InvalidArgumentException' with message 'Trailing data' … Carbon/Carbon.php:392
Question: How should I convert the above formatted date in php so Carbon accepts it?
I don't need to record seconds, so my Laravel model handles dates like so:
$this->attributes['date'] = Carbon::createFromFormat('Y-m-d H:i', $date);
Here's what I tried (with no success) up until now. I'm obviously missing something and not sure what i'm doing.
/**
* Store updates to event.
*
* @return Response
*/
public function update($id)
{
$event = Event::findOrFail($id);
$date = Request::get('jsdateobject');
// ------------------------------------------------------------
// trying to handle following format: 2015-08-19T11:00:00.000Z
// ------------------------------------------------------------
// $date = Carbon::parse($date)->toATOMString(); // didn't work - outputs: 2015-08-19T11:00:00+00:00
// $date = Carbon::parse($date)->toDateTimeString(); // didn't work - outputs: 2015-08-19 11:00:00
// $date = Carbon::parse($date)->toW3cString(); // didn't work - outputs: 2015-08-19T11:00:00+00:00
// $date = new Carbon($date); // didn't work - outputs: 2015-08-19 11:00:00
$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uO', $date); // didn't work - outputs: 2015-08-19 11:00:00
$event->date = $date;
$event->update();
return Response::json(array('success'=>true));
}
Date()
and then useCarbon::createFromTimestamp()
? – Audi