2016-11-09 146 views
0

我正在使用fullcalendar.js,我遇到了时区问题。 从我居住的地方开始工作得很好,但我的客户居住在加拿大伯灵顿并没有得到他点击的同一日期。我已将该区域设置为-05:00(他们的区域),但没有帮助,有没有办法将时区设置为本地?因此无论您在哪里访问它显示的完整日历。全日历日期在不同的国家有所不同

enter image description here

被白标明这些日期显示为6在加拿大,在同一天点击。

这是我用来打开dayclick

dayClick: function(date, jsEvent, view) { 
    var click = date._d; 
    var month = new Array(); 
    month[0] = "January"; 
    month[1] = "February"; 
    month[2] = "March"; 
    month[3] = "April"; 
    month[4] = "May"; 
    month[5] = "June"; 
    month[6] = "July"; 
    month[7] = "August"; 
    month[8] = "September"; 
    month[9] = "October"; 
    month[10] = "November"; 
    month[11] = "December"; 
    var weekday = new Array(7); 
    weekday[0]= "Sunday"; 
    weekday[1] = "Monday"; 
    weekday[2] = "Tuesday"; 
    weekday[3] = "Wednesday"; 
    weekday[4] = "Thursday"; 
    weekday[5] = "Friday"; 
    weekday[6] = "Saturday"; 
    $("#myModal").modal("show"); 

    document.getElementById("date").setAttribute("value", click.getFullYear() +"-"+ ("0"+ (click.getMonth()+1)).slice(-2)+"-"+("0" + click.getDate()).slice(-2)); 

    document.getElementById("month").setAttribute("value", month[click.getMonth()]); 
    document.getElementById("str_day").setAttribute("value", ("0" + click.getDate()).slice(-2)); 
    document.getElementById("end_day").setAttribute("value", weekday[click.getDay()]); 
    }, 
    eventResize: function(event, delta, revertFunc) { 
    console.log(event); 
    var title = event.title; 
    var end = event.end.format(); 
    var start = event.start.format(); 
    $.ajax({ 
     url: 'fullcalender/process.php', 
     data: 'type=resetdate&title='+title+'&start='+start+'&end='+end+'&eventid='+event.id, 
     type: 'POST', 
     dataType: 'json', 
     success: function(response){ 
     if(response.status != 'success') 
      revertFunc(); 
     }, 
     error: function(e){ 
     revertFunc(); 
     alert('Error processing your request: '+e.responseText); 
     } 
    }); 

模态代码,这是用于在数据库中

if($type == 'new') 
{ 
    $startdate = $_POST['startdate'].'+'.$_POST['zone']; 
    $title = $_POST['title']; 
    $insert = mysqli_query($con,"INSERT INTO calendar(`title`, `startdate`, `enddate`, `allDay`) VALUES('$title','$startdate','$startdate','false')"); 
    $lastid = mysqli_insert_id($con); 
    echo json_encode(array('status'=>'success','eventid'=>$lastid)); 
} 
+0

检查[这个苏答案(HTTP://计算器。 com/a/439871/2159528)根据UTC时间设置日期...如果它不能解决您的问题,则必须显示一些代码。 –

+0

我已经添加了代码:) –

回答

1

至于添加为您的存储时间的代码,他们应该被存储为UTC时间戳。为了检索时区,我使用jstz来获取用户时区,就像这两行一样简单。

var tz = jstz.determine(); 
var timezone = tz.name(); 

这会给你像“美国/芝加哥”无论位于何处,用户是一个时区,因为你不必问他们的时区的用户,它会在任何地方工作,这是伟大的。然后将您的timezone与您对事件的请求一起传递,并且您可以在检索事件时使用服务器端语言从UTC转换为提供的时区。

events: { 
    url: "calendarManager.php?request=get&timezone=" + timezone + "&userid=" + userID 
}, 

你也应该在你的日历配置使用

timezone: "local", 

,然后再返回使用上述方法事件时从UTC转换为本地用户的时区。

+0

我一直在尝试在fullcalendar.js中找到此行,但无法找到此:/ –

+0

你是什么意思?该配置不会在fullcalendar.js中设置,您将在初始化日历时将其设置为配置选项。 – Ryan89

+0

哦谢谢:) 虽然我做了一个临时的解决方案,因为客户端可以看到他点击的上一个日期,所以我在日期中添加了1,但我知道这不是永久性的方式,所以想要做到这一点永久:) –

0

我居然发现问题后3天, 问题是我使用GETDATE(), 使用getUTCDat()解决了这一问题:)