checkdate() : Checking a valid date entered by user in PHP

<?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.

Using checkdate()

We will try checkdate() function which takes care of leap year checking also. This function validates the date and returns true if date is correct or false if date is wrong or does not exist. Here is the format

If you are using the drop down date combination for selection or asking to enter date in a format, better to validate date by using checkdate function.

Here is the case where checkdate will return false
$m='11';
$d='31';
$y='05';
If(!checkdate($m,$d,$y)){
echo 'invalid date'; 
}else {
echo "Entry date is correct";
}

Input date from a text field

If we are asking the user to enter date in a text field then we have to break the entered date value by using explode function and then use the checkdate function to validate the date data ( of user ). Here we are collecting the user entered date value of a form posted by POST method.
$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

Input date from a calendar

We don't expect a wrong entry by user when they select a date from a Calendar. However it is better to check the date as user can change the dates after selecting from a calendar by using the above code for textbox.

How to pass date ( to text field) by using calendar using PHP & JavaScript

How to pass date ( to text field) by using a calendar using JQuery UI

Date Validation using createFromFormat()

We can create date objects by using createFromFormat() by entering date and format used.
<?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)
?>

Using date_get_last_errors()

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 Functions Timestamp in PHP Displaying calendar for selection of date by user

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    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?

    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer