Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm utterly stuck trying to make php api exchange dates with angular frontend.

From PHP to JS I seem to have it sorted. Since Laravel handles dates through Carbon I just added \Carbon\Carbon::setToStringFormat('c'); to app.php which makes dates come out in ISO 8601.

PHP example:

2015-02-04T00:53:51+02:00

AngularJS date filter also seems to understand this format well and even reads the timezone correctly.

What I have yet to get working is posting JS date objects back to PHP api.

JS example:

2015-02-05T13:00:00.000Z

Javascript date format ends up with milliseconds added to the string and in default configuration Carbon complains about trailing data.

Also browsers seem to automatically translate my dates into UTC time.

Manually using new Carbon('jsDateString') seems to work at first but on closer inspection timezone data is not considered.

So my question is what would be the best and most automatic solution to send dates back to Laravel php api from AngularJS frontend?

share|improve this question
    
Javascript doesn't have native support for timezones, which makes date handling kind of really annoying at times. You may want to take a look at momentjs.com which is a really good time manipulation library. This article (nulogy.com/articles/…) also describes the problems, and how they went about fixing it. Good luck! – Joel Hinz Feb 5 '15 at 13:14
    
Since I'm using LumX frontend gui framework I think momentJs is already tangled in there. Timezone wouldn't even be important if what I suspect is the browser wouldn't automatically convert my times to UTC. Thanks for the article. I will work through it and report back. – Priit Feb 5 '15 at 13:25
    
@JoelHinz thanks for the link. It has very good explanation of the problem and good reasoning how to solve it. But since I'm actually not that interested in timezones but am forced to deal with them just because somewhere along the way date entered in browser ends up in UTC I will look into the simpler moment(datetime).format() solution. – Priit Feb 5 '15 at 14:02
up vote 7 down vote accepted
$dt = Carbon::now();
echo $dt->toW3cString();       // 2015-02-05T14:50:55+01:00

since carbon can print it it can also parse this format

Carbon::parse('2015-02-05T14:50:55+01:00');

in javascript with moment.js momentjs.com

moment().format(); // 2015-02-05T14:50:55+01:00
share|improve this answer
    
Can't believe correctTime = moment(utcTime).format(); was all I needed to get the string back into correct timezone. Thank You @micha for the answer. Well this was a day well spent. – Priit Feb 5 '15 at 14:05
2  
As a caveat this still needs attribute setter for eloquent to fully recogize the format since the way it has been set up to handle incoming dates does not include this as an option public function setStartsAtAttribute($value) { $this->attributes['starts_at'] = Carbon::parse($value); } link – Priit Feb 5 '15 at 14:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.