I’m using jQuery widgets DatePicker and Dialog to interact with fullCallendar by Adam Shaw http://arshaw.com/fullcalendar. I have a lot of things working. I can use the DatePicker to jump to a specific date in fullcalendar. When I make a selection to book an event, the jQuery Dialog comes up and I use an ajax call to send the fields to a php script to process the data.
My problem, however, comes from the UNIX timestamps used in fullcalendar’s start and end variables. I set a global variable at the top of my html file, and set the start/end times from the selection in fullcalendar so that the data is passed around to the Dialog. From Dialog, I pass it to my PHP script. But when I convert that UNIX timestamp to a date in format ‘YmjHis’, I get weird results.
Here is the relevant code from the “select” method in fullcalendar:
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
}
},
Now, in the dialog does this (which is mostly taken directly from the jQuery UI examples page:
$( "#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" );
}
});
Ok, now my bookings.php file just has the following test code:
$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);
When I look at my variables.txt file, I see this:
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
What I would expect is for the start and end times to have a format like: 20120613113000, oh and not end with a 1969 date.
So I’m obviously doing something wrong in my conversions. This is the last hurdle to a full implementation of fullCalendar to do what I need it to do, and I’m stumped. Any help is greatly appreciated.
Right now it looks like there are are too many zeros in your start time and end time. My guess is it getting padding from some reason when you pull it out of the request.
Unix Conversion
StartTime
1339610400000 = 07/24/20 @ 6:00:00pm EST in (M/D/Y @ h:m:s)
EndTime
1339617600000 = 10/16/20 @ 2:00:00am EST in (M/D/Y @ h:m:s)
I’m thinking that you are working with date not in 2020 but in 2012 so I shorten the numbers above down to 10 digits (current TS as of 6/12/2012 @ 12:57pm = 1339610258 )
Start Time
1339610400 = 06/12/12 @ 1:00:00pm EST
End Time
1339617600 = 06/13/12/ @ 3:00:00pm EST
Now that looks more like a date you would book in a calendar.
My guess is you are getting extra zeros some where in your date.