如何处理PHP(Laravel API)和javascript(AngularJS)之间的日期时间 [英] How to handle datetime between php (Laravel api) and javascript (AngularJS)

查看:50
本文介绍了如何处理PHP(Laravel API)和javascript(AngularJS)之间的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全想尝试用尖角前端使php api交换日期.

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

从PHP到JS,我似乎都对其进行了排序.由于Laravel通过Carbon处理日期,因此我在app.php中添加了 \ Carbon \ Carbon :: setToStringFormat('c'); ,使日期符合ISO 8601.

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示例:

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

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

AngularJS日期过滤器似乎也很好地理解了这种格式,甚至可以正确读取时区.

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

我还没有开始工作,就是将JS日期对象重新发布到PHP api.

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

JS示例:

2015-02-05T13:00:00.000Z

2015-02-05T13:00:00.000Z

Javascript日期格式以添加到字符串中的毫秒结尾,并且在默认配置下,Carbon抱怨尾随数据.

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

另外,浏览器似乎也会自动将我的日期转换为UTC时间.

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

首先手动使用 new Carbon('jsDateString')似乎可以,但是在更仔细的检查时区数据上不被考虑.

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

所以我的问题是,从AngularJS前端将日期发送回Laravel php API的最佳,最自动化的解决方案是什么?

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

推荐答案

$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');

在带有moment.js的javascript中 momentjs.com

in javascript with moment.js momentjs.com

moment().format(); // 2015-02-05T14:50:55+01:00

这篇关于如何处理PHP(Laravel API)和javascript(AngularJS)之间的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆