jQuery UI: Highlight multiple dates in jquery datepicker
The events are stored as UNIX timestamps in a MySQL table. We’ll be getting those events from the database via AJAX and highlight them in the jQuery UI datepicker widget.
HTML
Javascript
$.ajax({
url: "dates.php",
data: "action=showdates",
dataType: "json",
success: function(calendarEvents){
$(".calendarwidget").datepicker({
// [rows, columns] if you want to display multiple calendars.
numberOfMonths: [1, 1],
showCurrentAtPos: 0,
beforeShowDay: function (date){
for (i = 0; i < calendarEvents.length; i++) {
if (date.getMonth() == calendarEvents[i][0] - 1
&& date.getDate() == calendarEvents[i][1]
&& date.getFullYear() == calendarEvents[i][2]) {
//[disable/enable, class for styling appearance, tool tip]
return [false,"ui-state-active","Event Name"];
}
}
return [true, ""];//enable all other days
}
});
}
});
PHP
Here is our AJAX service. Assumption here is that we are getting the date from a UNIX timestamp stored in a MySQL table.
$action = $_REQUEST['action'];
switch ($action){
case 'showdates':
$qry = "SELECT * FROM event_table";
$result = mysql_query($qry) or die ("Error in query: $qry. " . mysql_error());
echo '[';
do{
if($row->eventid != ''){//makes sure no blanks are returned
//this assumes that "eventdate" is UNIX timestamp
$converteddate = date("n,j,Y",$row->eventdate);
$dates .= '['.$converteddate.'],';
}
}while($row = mysql_fetch_object($result));
$dates =rtrim($dates, ",");
echo $dates;
echo ']';
break;
}
Resources
jQuery UI datepicker
http://jqueryui.com/demos/datepicker/
jQuery AJAX
PHP Date Manual
http://php.net/manual/en/function.date.php
Example
Download
Do you like this script? Support it by downloading the packaged file with an example:

C
thank you very much for the code, has helped me a lot, I have implemented a project I’m currently working, very useful
eplicanic
Cool! I’m always happy to hear I could help!!!
Mihail
Could you, please, format this code in something like demo page?
eplicanic
Here you go! I just added a demo to the post!
Andre
Very nice demo, thank you
I have one question:
If we have 2 Datepickers, i would like that they count automatically if i change one datepicker ( no negative days for counting nights or autoselcet the next day)
Is there any solution for that what i mean?
thank you
Rumaan
Looking for a jquery datepicker with multiple dates being selected(dynamically) and clickable. on every date highlited want to attach some unique id with it.. so when the user clicks that date, we get the unique id and go to db….
Any clue?
bhaskar
nice post .it was helped to me
Thank u
lagrandeours
hello
thnaks for your dowload,
how to enable multiSelect: 5 ?
thanks,