I made a patch for jsCalendar to make the Unix timestamp readily available when users choose a different date through the calendar. The timestamp is updated by the calendar itself and saved in a hidden field, and it can be read in the submit function by the $_POST variable (seems that the $form_values gets the original values for hidden fields, so the changes are available in $_POST only...). Here I explain my changes.
In the jscalendar.module I added the "timestamp" hidden field, and set the other fileds as disabled, as the submit doesn't need them.
function jscalendar_form_alter($form_id, &$form) {
........
<strong>$settings = array('timestamp', 'ifFormat', 'showsTime', 'timeFormat');</strong>
foreach ($settings as $setting) {
if (isset($form[$key]['#jscalendar_' . $setting])) {
$form[$key.'_jscalendar']['#tree'] = true;
$form[$key.'_jscalendar'][$setting] = array(
'#type' => 'hidden',
'#value' => $form[$key]['#jscalendar_' . $setting],
<strong>'#disabled' => ($setting != 'timestamp'), // disable values not necessary in form submit</strong>
);
unset($form[$key]['#jscalendar_' . $setting]);
}
}
........
}
In the jscalendar.js I passed the timestamp field as a parameter, and added the code to fire when the calendar is updated. The cal.date.print('%s') is the unix timestamp for the chosen date.
Calendar.setup(
{
inputField : this.id,
ifFormat : settings['ifFormat'],
button : id + '-button',
showsTime : settings['showsTime'],
timeFormat : settings['timeFormat'],
timestamp : this.form.elements[id + '_jscalendar-timestamp'],
onUpdate : calendarUpdated
}
);
function calendarUpdated(cal) {
// update timestamp hidden value
var timestamp = cal.params.timestamp;
if (timestamp) timestamp.value = cal.date.print('%s');
}
This is an example with the applied changes ($t is the unix timestamp, used in the default value and in the #jscalendar_timestamp parameter):
$form['start_date'] = array(
'#title' => t('Start Date'),
'#type' => 'textfield',
'#default_value'=>format_date($t, 'custom', 'd/m/Y'),
'#attributes' => array('class' => 'jscalendar'),
'#jscalendar_ifFormat' => '%Y-%m-%d %H:%M',
'#jscalendar_showsTime' => 'false',
'#jscalendar_timestamp'=>$t,
(...)
);
In the form submit function the unix timestamp is available by the $_POST variable:
$start_date = $_POST['startdate_jscalendar']['timestamp'];
| Comment | File | Size | Author |
|---|---|---|---|
| jscalendar.patch_1.txt | 6.99 KB | stefano73 |