This example combines jQuery UI Datepicker, PHP and MySQL to let users choose a date and view its events. An administrator page can add or delete events.

| Page | Purpose |
|---|---|
config.php | Database connection settings. |
display.php | Displays the Datepicker and shows events after the visitor chooses a date. |
display-data.php | Backend endpoint that returns events for the selected date. |
add-event/add.php | Admin page for choosing a date, adding events and deleting existing events. |
The original script builds an array of dates that already contain events. The Datepicker beforeShowDay option can use that array to decide whether each calendar date should be selectable.
Learn jQuery UI Datepicker · Open the beforeShowDay demo
const availableDates = [
"05-12-2026",
"25-12-2026",
"01-01-2027",
"02-02-2027"
];
Instead of manually concatenating JavaScript text, fetch the dates in PHP and pass the array through json_encode(). This preserves the original PHP-to-JavaScript technique while avoiding malformed output.
<?php
$sql = "
SELECT DISTINCT DATE_FORMAT(date, '%d-%m-%Y') AS event_date
FROM plus2net_event
ORDER BY date
";
$availableDates = [];
foreach ($dbo->query($sql) as $row) {
$availableDates[] = $row['event_date'];
}
?>
<script>
const availableDates =
<?php echo json_encode($availableDates, JSON_UNESCAPED_SLASHES); ?>;
</script>
The Datepicker onSelect callback receives the selected date. The example then uses jQuery .load() to request the backend page and place the returned event list inside #d1.
$( "#date_picker" ).datepicker({
dateFormat: "dd-mm-yy",
onSelect: function (selectedDate) {
const url =
"display-data.php?selectedDate=" +
encodeURIComponent( selectedDate );
$( "#d1" ).load( url );
}
});
Validate the incoming date before querying the database. The following example converts the expected dd-mm-YYYY value to MySQL's YYYY-mm-dd format.
<?php
$input = $_GET['selectedDate'] ?? '';
$date = DateTime::createFromFormat('d-m-Y', $input);
if (!$date || $date->format('d-m-Y') !== $input) {
http_response_code(400);
exit('Invalid date');
}
$mysqlDate = $date->format('Y-m-d');
?>
The original script placed the date directly into the SQL string. For a current PHP example, use a prepared statement.
<?php
$stmt = $dbo->prepare(
'SELECT event_id, date, detail
FROM plus2net_event
WHERE date = :event_date
ORDER BY event_id'
);
$stmt->execute(['event_date' => $mysqlDate]);
foreach ($stmt as $row) {
echo htmlspecialchars($row['date'], ENT_QUOTES, 'UTF-8');
echo ': ';
echo htmlspecialchars($row['detail'], ENT_QUOTES, 'UTF-8');
echo '<br>';
}
?>
The administration page at add-event/add.php lets an administrator choose a date, add event details, list existing events and delete an event. See the PDO insert tutorial for the database-insert pattern.
The downloadable project intentionally does not include a login system. If this is used on a public site, protect the administration page with authentication and authorization.
CREATE TABLE plus2net_event (
event_id INT UNSIGNED NOT NULL AUTO_INCREMENT,
date DATE NOT NULL,
detail VARCHAR(256) NOT NULL,
PRIMARY KEY (event_id),
INDEX idx_event_date (date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
config.php and enter your database connection details.add-event/add.php and add a few events.display.php and choose a date that has an event.Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| ishanishani | 06-07-2016 |
| Thanks for the code. Download zip file not working. plz can you send it... or correct it. | |
| smo1234 | 19-02-2017 |
| Corrected zip file is available now, you can download. | |
11-09-2021 | |
| Thank you Your code was very helpful! | |