Validate the three numeric parts first, then pass month, day and year to checkdate(). This rejects impossible dates such as 31 February.
$month = filter_input(INPUT_POST, 'month', FILTER_VALIDATE_INT);
$day = filter_input(INPUT_POST, 'day', FILTER_VALIDATE_INT);
$year = filter_input(INPUT_POST, 'year', FILTER_VALIDATE_INT);
if ($month && $day && $year && checkdate($month, $day, $year)) {
echo 'Valid date';
} else {
echo 'Invalid date';
}
<?php
echo checkdate(12,31,2017); // Output 1
echo var_dump(checkdate(12,31,2017)); // Output bool(true)
echo var_dump(checkdate(29,02,2017)); // Output bool(false)
echo var_dump(checkdate(06,31,2023)); // Output bool(false)
?>
Syntax
bool checkdate(int $month, int $day, int $year)
Many times we have to check the date entered are in correct format or not. The combination of entered month date and year by a user has to be a valid date to use in our applications. Even if we give a selection or a drop down list box to select a date we have to check the combination of month, day and year selection is valid or not. User may select 29th Feb 2005 (which is not a leap year ) or it may select 31st Nov of any year. So the combination has to be checked.
$m='11';
$d='31';
$y='05';
If(!checkdate($m,$d,$y)){
echo 'invalid date';
}else {
echo "Entry date is correct";
}
$dt = filter_input(INPUT_POST, 'dt');
$dt = $dt ?: '02/28/2007'; // m/d/Y example
$arr = explode('/', $dt);
if (count($arr) === 3) {
[$mm, $dd, $yy] = array_map('intval', $arr);
echo checkdate($mm, $dd, $yy) ? 'Entry date is correct' : 'Invalid date';
} else {
echo 'Use m/d/Y format';
}
If the input uses d/m/Y instead, map the array positions accordingly:
$dd=$arr[0]; // first element of the array is date
$mm=$arr[1]; // second element is month
$yy=$arr[2]; // third element is year
<?php
function Date_validate($input_date, $format = 'd-m-Y')
{
$date_obj = DateTime::createFromFormat($format, $input_date);
return $date_obj && $date_obj->format($format) == $input_date;
}
echo Date_validate('12-12-2017'); // Output is 1
echo var_dump(Date_validate('12-12-2017')); // Output is bool(true)
echo var_dump(Date_validate('31-11-2017')); // Output is bool(false)
?>
Getting error or warning messages by using date_get_last_errors().
$date = DateTime::createFromFormat('d/m/Y', '01/13/2019');
// change above input date for different messages. //
$errors = DateTime::getLastErrors();
if (!empty($errors['warning_count'])) {
echo "Strictly speaking, input date is invalid! ( Warning ) ";
}
if (!empty($errors['error_count'])) {
echo "input date is invalid! ( error ) ";
}
//echo "<br>Input date is : ".$date->format('Y-m-d H:i:s');
01/13/2019 does not match d/m/Y. createFromFormat() may normalize the value, so always inspect getLastErrors() before accepting the result.
For completely invalid text such as Wrong data, parsing fails; treat a false return value or reported errors as invalid input.
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.
| Mat | 09-02-2009 |
| how to compare the data entered by the user to the data in the database? | |
| smo | 10-02-2009 |
| It can be used in a sql query. A link is added now on this at the end of the tutorial above. | |
| jisha | 28-06-2009 |
| how to check a date already entered in database is within the range of a date and duration currently entered by a user? | |
| smo | 29-06-2009 |
| In SQL section you can read this article on how to get record between two dates. | |
| tonier | 04-09-2009 |
| Just remember this, in SQL, valid format is 'Y-m-d' and day and month must be in 2 digits char. | |
| ikram | 02-05-2013 |
| send email befor one day to friends birthday | |
| Mohammad | 17-08-2013 |
| How To Find Days From Two Date ? Is there any specific function like floor or else? | |