javascript FullCalendar 时区不会在客户端修改时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21594825/
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
FullCalendar timezones not modifying times on client's end
提问by What have you tried
I am experiencing a frustrating issue which I've been working on all night. Long story short, if someone in New York creates an event at 2Pm, I'd like that event to show as 8Pm for someone viewing the calendar in Europe (they are six hours ahead let's say).
我遇到了一个令人沮丧的问题,我整夜都在研究这个问题。长话短说,如果纽约的某个人在下午 2 点创建一个活动,我希望该活动显示为下午 8 点,以便在欧洲查看日历的人(比方说他们提前了六个小时)。
My fullcalendar configs look like this:
我的全日历配置如下所示:
function renderSchedule(timezone) {
var userSchedule = $('#user-schedule').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
timezone: timezone,
ignoreTimezone: false,
events: baseurl + "load_schedule",
editable: true,
defaultView: 'agendaWeek',
firstHour: 8,
allDayDefault: false
}
I do have more options, but those should suffice for the purpose of this question.As mentioned in the docs, fullcalendar passes a URL via GET which looks like this:
我确实有更多的选择,但这些应该足以满足这个问题的目的。正如文档中提到的,fullcalendar 通过 GET 传递一个 URL,如下所示:
mysite.com / my_page / load_schedule ? start = 2014 - 02 - 02 & end = 2014 - 02 - 09 & timezone = Europe % 2FChisinau & _ = 1391660233815
Then, on the backend I am doing the following to convert fromMYSQL datetime
toISO8601
然后,在后端,我正在执行以下操作以从转换MYSQL datetime
为ISO8601
$start = ($event['start']);
$end = ($event['end']);
$dateStart = new DateTime($start, new DateTimeZone($timezone));
$dateEnd = new DateTime($end, new DateTimeZone($timezone));
$dateStart - > setTimezone(new DateTimeZone($timezone));
$dateEnd - > setTimezone(new DateTimeZone($timezone));
$event['start'] = $dateStart - > format(DateTime::ISO8601);
$event['end'] = $dateEnd - > format(DateTime::ISO8601);
$newResult[] = $event;
The $timezone
variable holds Europe/Chisinau
which was of course passed by fullcalendar itself.
该$timezone
变量保存Europe/Chisinau
这是当然的fullcalendar本身传递的。
Here is what the above returns back to fullcalendar:
这是上面返回到 fullcalendar 的内容:
[{"id":"5","start":"2014-02-06T14:10:00+0200","end":"2014-02-06T15:00:00+0200","title":"My title"}]
Everything seems to be correct, but regardless of what I've tried the events show as 2PM.
一切似乎都是正确的,但无论我尝试过什么,事件都显示为 2PM。
NOTEThis is important, I haven't mentioned this yet but I am passing in a timezone of my choice to the renderSchedule
function. So, even though I am located on the East Coast of the US, I would like to be able to pass in a European Timezone and have the calendar render accordingly.
注意这很重要,我还没有提到这一点,但我将我选择的时区传递给renderSchedule
函数。因此,即使我位于美国东海岸,我也希望能够传入欧洲时区并相应地呈现日历。
Also, I should point out that I'm using the beta version (2.0) which allows a timezone string to be passed (which I am doing) and uses moments via moment.js
另外,我应该指出,我使用的是测试版 (2.0),它允许传递时区字符串(我正在这样做)并通过moment.js使用时刻
Any idea what I'm doing wrong here?
知道我在这里做错了什么吗?
采纳答案by MarCrazyness
I was able to get it to work from the demo. I updated your date format to include colons for the offset. Also the latest stable version of fullcalendar 1.6.4 doesn't have a timezone variable.
我能够从演示中让它工作。我更新了您的日期格式以包含偏移量的冒号。此外,fullcalendar 1.6.4 的最新稳定版本没有时区变量。
$('#calendar').fullCalendar({
editable: true,
header : {
left:"prev,next",
center:"title",
right:"month,basicWeek,agendaWeek,basicDay,agendaDay"
},
ignoreTimezone: false,
events: [
{
start: "2014-02-06T14:10:00+02:00",
end:"2014-02-06T15:00:00+02:00",
title:"My title",
allDay: false
}
});
If you are using the beta release 2.0, then you need to remove ignoreTimezone as this is no longer a parameter.
如果您使用的是 beta 版本 2.0,那么您需要删除 ignoreTimezone,因为它不再是一个参数。
Updated Answer:
更新答案:
It turns out the issue was how the dates were being handled on the backend. Here is how I solved this problem.
事实证明,问题在于后端如何处理日期。这是我解决这个问题的方法。
date_default_timezone_set($timezone);
$timestampStart = strtotime($mysql_start_date);
$timestampEnd = strtotime($mysql_end_date);
$local_time_start = $timestampStart + date('Z');
$local_time_end = $timestampEnd + date('Z');
$local_date_start = date('Y-m-d H:i:s', $local_time_start);
$local_date_end = date('Y-m-d H:i:s', $local_time_end);
$event['start'] = $local_date_start;
$event['end'] = $local_date_end;