Handle datetime between php (Laravel API) and javascript (AngularJS)
Asked Answered
A

1

8

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));
}       
Announce answered 20/8, 2015 at 15:16 Comment(4)
Could you convert it to a unix timestamp from JS Date() and then use Carbon::createFromTimestamp()?Audi
I didn't want to play with the date format in JS (besides just updating it), so I was trying to manipulate the dates in my Laravel controller. I played with Carbon::createFromFormat with no luck (i've updated code above).Announce
I think I got it now. Question updated with solutionAnnounce
please post your answer in answers else I will make an edit here.. thanksZenaidazenana
Z
1

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.

Zenaidazenana answered 2/10, 2017 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.