我正在使用 jQuery 小部件 DatePicker 和 Dialog 与 Adam Shaw 的 fullCallendar 进行交互 http://arshaw.com/fullcalendar 。我有很多事情要做。我可以使用 DatePicker 跳转到全日历中的特定日期。当我选择预订事件时,会出现 jQuery 对话框,我使用 ajax 调用将字段发送到 php 脚本来处理数据。
但是,我的问题来自 fullcalendar 的开始和结束变量中使用的 UNIX 时间戳。我在 html 文件的顶部设置了一个全局变量,并设置了 fullcalendar 中选择的开始/结束时间,以便将数据传递到对话框。我从 Dialog 将其传递给我的 PHP 脚本。但是当我将该 UNIX 时间戳转换为“YmjHis”格式的日期时,我得到了奇怪的结果。
以下是 fullcalendar 中“select”方法的相关代码:
select: function(start, end, allDay) {
// need to check the day first. If the selected day/time < today, throw an alert
// otherwise allow the booking of the conference.
var now = calendar.fullCalendar('getDate');
if (start < now )
{
alert('You cannot book a conference in the past!');
calendar.fullCalendar( 'unselect' );
}
else
{
// set the global variables
st = start;
et = end;
$('#dialog-form').dialog('open'); // open the dialog form
}
},
现在,在对话框中执行此操作(主要直接取自 jQuery UI 示例页面:
$( "#dialog-form" ).dialog({
autoOpen: false,
height: 300,
width: 350,
modal: true,
buttons: {
"Create Event": function() {
var bValid = true;
allFields.removeClass( "ui-state-error" );
bValid = bValid && checkLength( name, "name", 3, 25 );
bValid = bValid && checkLength( title, "title", 1, 20 );
bValid = bValid && checkLength( email, "email", 6, 80 );
bValid = bValid && checkLength( ports, "ports", 1, 2 );
bValid = bValid && checkRegexp( name, /^[a-z]([0-9a-z_])+$/i, "Name may consist of a-z, 0-9, underscores, begin with a letter." );
// From jquery.validate.js (by joern), contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
// change to ensure a domain email address
bValid = bValid && checkRegexp( email, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i, "eg. ui@jquery.com" );
if ( bValid ) {
// code to insert into DB goes here
// need to somehow grab either a global variable or access
// full calendar start/end times from the selection phase
// in order to pass to the DB.
$.ajax(
{
url: "bookings.php",
type: "POST",
data: {e: email.val(), t: title.val(), n: name.val(), p: ports.val(), start: +st, end: +et},
dataType: "HTML",
success: function(data) {
$('.result').html(data);
// reload fullCalendar here maybe??
}
});
$( this ).dialog( "close" );
}
},
Cancel: function() {
$( this ).dialog( "close" );
}
},
close: function() {
allFields.val( "" ).removeClass( "ui-state-error" );
}
});
好的,现在我的 bookings.php 文件只有以下测试代码:
$file = 'variables.txt';
$arr= $_REQUEST;
$fp = fopen($file, 'w') or die('Could not open file!');
fwrite($fp, "variables are:\n");
foreach ($arr as $key => $value) {
$toFile = "Key: $key; Value: $value \n";
// write to file
fwrite($fp, "$toFile") or die('Could not write to file');
}
// DEBUG CODE write some blank space
fwrite($fp, "\n\n") or die('Could not write to file');
$startTime = $_REQUEST['start'];
$endTime = $_REQUEST['end'];
$start = date('YmjHis', $startTime);
fwrite($fp, "start time: $startTime = $start\n") or die('Could not write to file');
$end = date('YmjHis', $endtime);
fwrite($fp, "end time: $endTime = $end\n") or die('Could not write to file');
// close file
fclose($fp);
当我查看变量.txt 文件时,我看到以下内容:
Key: e; Value: me@blah.com
Key: t; Value: blah
Key: n; Value: blah
Key: p; Value: 2
Key: start; Value: 1339610400000
Key: end; Value: 1339617600000
start time: 1339610400000 = 444200724160000
end time: 1339617600000 = 19691231160000
我期望开始和结束时间的格式如下:20120613113000,哦,不要以 1969 年日期结束。
所以我显然在转换中做错了一些事情。这是完全实现 fullCalendar 来完成我需要它做的事情的最后一个障碍,我被难住了。非常感谢任何帮助。
最佳答案
现在看来您的开始时间和结束时间中有太多零。我的猜测是,当您将其从请求中拉出时,它会因某种原因而得到填充。
Unix 转换
开始时间
1339610400000 = 07/24/20 @ 6:00:00pm 美国东部标准时间 (M/D/Y @ h:m:s)
结束时间 1339617600000 = 10/16/20 @ 2:00:00am 美国东部时间 (M/D/Y @ h:m:s)
我认为您使用的日期不是 2020 年而是 2012 年,因此我将上面的数字缩短为 10 位(当前 TS 截至 6/12/2012 @ 12:57pm = 1339610258)
开始时间 1339610400 = 2012 年 6 月 12 日 @ 下午 1:00:00(美国东部时间)
结束时间 1339617600 = 06/13/12/@ 下午 3:00:00 美国东部时间
现在看起来更像是您在日历中预订的日期。
我的猜测是你在约会的某个地方得到了额外的零。
关于jquery - fullCalendar UNIX 时间戳转换问题与选择事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019405/