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));
}
Originally posted by questionaire himeself but he posted the answer into the question itself so here I'm editing the question to make more sense He wrote:
I finally go it.
After updating my datetime in my AngularJS front end and posting my JS date object back to my PHP API, I couldn't figure out how to use the following date format:
2015-08-19T11:00:00.000Z
I kept getting the exception
'InvalidArgumentException' with message 'Trailing data' … Carbon/Carbon.php:392 error.
I tried
$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uO', $date);
but that didn't work. After realizing that my Laravel Model was expecting the following datetime format Y-m-d H:i
, I had to double format the datetime received from JS.
$date = Carbon::createFromFormat('Y-m-d\TH:i:s.uO', $date);->format('Y-m-d H:i');
Not sure it's the best way to do it, but it works for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With