Choose a start date, an end date, or both. The tool builds the date-range part of a SQL query. For DATETIME columns, use a half-open upper boundary when the input represents whole calendar dates.
SELECT *
FROM table_name
WHERE dt >= '2026-09-01'
AND dt < '2026-09-11';
If the user selects September 10 as the final calendar date and dt is a DATETIME column, using the next day as an exclusive upper boundary includes every time on September 10. See date-range queries for the reasoning.
For real database searches, do not concatenate submitted dates into SQL. Validate the values and bind them as parameters.
<?php
$dt1 = $_POST['dt1'] ?? '';
$dt2 = $_POST['dt2'] ?? '';
$conditions = [];
$params = [];
if ($dt1 !== '') {
$from = DateTimeImmutable::createFromFormat('!Y-m-d', $dt1);
if ($from === false || $from->format('Y-m-d') !== $dt1) {
throw new InvalidArgumentException('Invalid start date.');
}
$conditions[] = 'dt >= :from_date';
$params[':from_date'] = $dt1;
}
if ($dt2 !== '') {
$to = DateTimeImmutable::createFromFormat('!Y-m-d', $dt2);
if ($to === false || $to->format('Y-m-d') !== $dt2) {
throw new InvalidArgumentException('Invalid end date.');
}
$nextDay = $to->modify('+1 day')->format('Y-m-d');
$conditions[] = 'dt < :to_date';
$params[':to_date'] = $nextDay;
}
$sql = 'SELECT * FROM table_name';
if ($conditions) {
$sql .= ' WHERE ' . implode(' AND ', $conditions);
}
$stmt = $dbo->prepare($sql);
$stmt->execute($params);
?>
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.
| saravanan | 27-06-2014 |
| if compare two dates that particular values is changed on rows and column by using stored procedure and displayed on gridview using C# with example and explaination | |
| DR NIKHIL KUMAR GHORAI | 14-02-2017 |
| how to execute the statement and show data in a table injsp | |