douou9094747 2014-02-06 05:32
浏览 117
已采纳

FullCalendar时区不会修改客户端的时间

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).

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:

         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

$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.

Here is what the above returns back to 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.

NOTE This 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.

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

Any idea what I'm doing wrong here?

  • 写回答

1条回答

  • dqluw20882 2014-02-06 15:59
    关注

    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.

    $('#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.

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)