4

We are having problems to get the datetime representation in php and momentjs in sync. Lets say we are having a datetime with a time zone in Moscow:

2013-06-10T10:40:00.000+04:00

The user should see the time in local: 2013-06-10T10:40:00. No matter if he sits in Spain or USA. Following php code would produce the proper time that we need:

$date = new DateTime('2013-06-10T10:40:00.000+04:00');
echo $date->format('d.m.Y H:i:s');

The output is:

10.06.2013 10:40:00

But if we parse the same datetime string in frontend with momentjs:

moment('2013-06-10T10:40:00.000+04:00').format('D.M.YYYY h:mm:ss');

The output is:

10.6.2013 8:40:00

The browsers time zone is Europe/Berlin (+02:00). Using utc() calculates the date the wrong way also. What we need is a local time of the place, so in case of php it is the right one. Giving a parse string to momentjs:

moment('2013-06-10T10:40:00.000+04:00', "YYYY-MM-DD HH:mm").format('D.M.YYYY h:mm:ss')

would do the trick, so the time zone is deleted. But we actually should set the parse string globally, but how?

Thanks for suggestions.

4

2 回答 2

5

我相信你会受到moment.js 问题 611的影响——这实际上是一个功能请求,因为 JavaScript 本身并不会这样做。问题评论中有一些可以接受的解决方法。请在那里添加您自己的反馈。谢谢。

更新

随着moment.js版本 2.1.0 的发布,您现在可以这样做:

var input = "2013-06-10T10:40:00.000+04:00";
var m = moment(input).zone(input);
var s = m.format('D.M.YYYY h:mm:ss');

额外的调用.zone(input)将仅从字符串中提取偏移部分 ( +04:00) 并将其设置为用于格式化的区域。还有其他选择。请参阅文档中的moment#zone

更新#2

随着moment.js版本 2.3.0 的发布,现在这是一个调用:

var m = moment.parseZone(input);
于 2013-04-23T19:49:27.560 回答
0

尝试将 PHP 时区设置为 Europe/Berlin

 date_default_timezone_set("Europe/Berlin")

可能是您的 PHP 安装/服务器使用不同的时区。

于 2013-04-23T19:25:38.507 回答