I am trying to make my Laravel API exchange dates with my Angularjs interface.
It works from Laravel to JS, first converting the 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 : sending 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 it gets:
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 do I convert the above formatted date to php so that Carbon accepts it?
I don't need to record seconds, so my Laravel model handles dates:
$this->attributes['date'] = Carbon::createFromFormat('Ymd H:i', $date);
Here is what I have tried (without success) so far. I obviously missed something and am not sure what I am 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('Ymd\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)); }
datetime laravel laravel-5 laravel-4 php-carbon
user3489502
source share