How can I allow specific dates?

Datepicker with specified dates
If you have, for example, a site where some events are only available on certain days it would be useful to be able to have only those dates available to be selected in a datepicker.
Here's an example of some code used to do that using the MooTools DatePicker. In this example we are reading the dates when visits to a particular zip code have been scheduled. The database columns are `azipcode` and `adate` and the date is formatted as YYYY-MM-DD:
 
<?php
// query the database
$db =& JFactory::getDBO();
$query = "
  SELECT DISTINCT `azipcode`, `adate` 
    FROM `#__chronoforms_data_addapickupdate`
    WHERE `adate` > CURDATE()
    ORDER BY `azipcode` ;
";
$db->setQuery($query);
$data = $db->loadObjectList();
// build an array of dates for each zipcode
$dates = array();
foreach ( $data as $v ) {
  $d = explode('-', $v->adate);
  $dates[$v->azipcode][$d[0]][$d[1]][] = $d[2];
}
// convert the dates to strings for the MooTools DatePicker
$strings = array();
foreach ( $dates as $k => $v ) {
  $strings[$k] = str_replace('"', '', json_encode($v));
}
// select the string for zipcode = $x (some code omitted here)
echo "var date_string = {$strings[$x]};";
?>
Then in the Custom MooTools DatePicker action we can set the availalbleDates option:
availableDates: date_string,

Note that the query only gets future dates from the database and you might also want to set a maxDate a few months in the future to keep the date range string to a reasonable size. 

You may also like:

Comments:

You need to login to be able to post a comment.