Handling date and time between php (Laravel API) and javascript (AngularJS)

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)); } 
+8
datetime laravel laravel-5 laravel-4 php-carbon
source share
1 answer

Originally sent by a questionnaire, but he sent the answer to the question himself, so here I am editing the question to make more sense. He wrote:

I will finally go.

After updating my datetime in my front end of AngularJS and sending the JS date object back to my PHP API, I could not figure out how to use the following date format:

 2015-08-19T11:00:00.000Z 

I kept getting an exception

'InvalidArgumentException' with the message "Trailing data" ... Error Carbon / Carbon.php: 392.

I tried

 $date = Carbon::createFromFormat('Ymd\TH:i:s.uO', $date); 

but it didn’t work. Realizing that my Laravel model expects the following datetime format Ymd H:i , I had to double the date and time format received from JS.

 $date = Carbon::createFromFormat('Ymd\TH:i:s.uO', $date);->format('Ymd H:i'); 

Not sure if this is the best way to do this, but now it works.

0
source share

All Articles