<?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=$_POST['dt'];
$dt="02/28/2007"; // Setting a date in m/d/Y format
$arr=explode("/",$dt); // breaking string to create an array
$mm=$arr[0]; // first element of the array is month
$dd=$arr[1]; // second element is date
$yy=$arr[2]; // third element is year
If(!checkdate($mm,$dd,$yy)){
echo "invalid date";
}else {
echo "Entry date is correct";
}
If your input format is different then you can change the variables after creating the array by.
$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');
$date = DateTime::createFromFormat('d/m/Y', '01/13/2019');
Strictly speaking, input date is invalid! ( Warning )
Input date is : 2020-01-01 12:34:34
$date = DateTime::createFromFormat('d/m/Y', 'Wrong data');
input date is invalid! ( error )
PHP Date FunctionsTimestamp in PHP Displaying calendar for selection of date by user
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? |