如何处理php(Laravel api)和javascript(AngularJS)之间的日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28344550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:50:32  来源:igfitidea点击:

How to handle datetime between php (Laravel api) and javascript (AngularJS)

javascriptphpangularjsdatelaravel

提问by Priit

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

我完全无法使用角度前端使 php api 交换日期。

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 到 JS 我似乎把它排序了。由于 Laravel 通过 Carbon 处理日期,我刚刚添加\Carbon\Carbon::setToStringFormat('c');到 app.php 中,这使得日期在 ISO 8601 中出现。

PHP example:

PHP 示例:

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

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

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

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

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

我还没有开始工作是将 JS 日期对象发布回 PHP api。

JS example:

JS示例:

2015-02-05T13:00:00.000Z

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.

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

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

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

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

手动使用new Carbon('jsDateString')起初似乎有效,但仔细检查时区数据并未考虑在内。

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

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

回答by micha

$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 可以打印它也可以解析这种格式

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

in javascript with moment.js momentjs.com

在 JavaScript 中使用 moment.js momentjs.com

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

回答by Yevgeniy Afanasyev

1) make a Carbon date in Laravel controller

1) 在 Laravel 控制器中制作碳日期

$date_from = Carbon::now();
return view('reports', compact('date_from'));

2) You need to init Date in Angular conctoller

2) 您需要在 Angular conctoller 中初始化日期

rrApp.controller('reportsCtrl', ['$scope', '$filter', function($scope, $filter)
{
        $scope.start_date = new Date("{{$date_from}}");
        $scope.start_date = new Date($filter("date")($scope.start_date, 'yyyy-MM-dd'));// only date, no time

}]); 

3) use it

3)使用它

<input type="date" class="form-control" ng-model="start_date">

4) Do NOT try to init the variable in ng-init, it would not work:

4) 不要尝试在 ng-init 中初始化变量,它不会工作: