我正在使用 jQuery 小部件 DatePicker 和 Dialog 与 Adam Shaw http://arshaw.com/fullcalendar的 fullCallendar 进行交互。我有很多工作要做。我可以使用 DatePicker 跳转到完整日历中的特定日期。当我选择预订活动时,会出现 jQuery 对话框,我使用 ajax 调用将字段发送到 php 脚本以处理数据。
然而,我的问题来自 fullcalendar 的开始和结束变量中使用的 UNIX 时间戳。我在 html 文件的顶部设置了一个全局变量,并在 fullcalendar 中设置了选择的开始/结束时间,以便将数据传递给对话框。从对话框中,我将它传递给我的 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);
当我查看 variables.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 来做我需要做的事情的最后一个障碍,我很难过。任何帮助是极大的赞赏。