If you know you’re going to be using a more modern version of PHP you can take advantage of the very cool and now native DateTime class. Technically available from PHP >= 5.2.0 it isn’t really worth using until >=5.3.0 due to bugs and missing features, (DateTime::createFromFormat or DateTime::getLastErrors in particular). Until recently there has not been a good, native way of doing this, yes checkdate() has been around for a while but it’s particularly fussy with it’s input, what we needed was a mixture of date() where we can specify the input format and checkdate() to validate it. echo "Input date is : ".More times than I can count I’ve had to validate dates in PHP, not only check if it is in the required format but also to make sure the date actually exists.
Įcho "Strictly speaking, input date is invalid! ( Warning ) " Įcho "input date is invalid! ( error ) " change above input date for different messages. $date = DateTime::createFromFormat('d/m/Y', '') Getting error or warning messages by using date_get_last_errors(). We can create date objects by using createFromFormat() by entering date and format used.Įcho Date_validate('12-12-2017') // Output is 1Įcho var_dump(Date_validate('12-12-2017')) // Output is bool(true)Įcho var_dump(Date_validate('31-11-2017')) // Output is bool(false) How to pass date ( to text field) by using a calendar using JQuery UI Date Validation using createFromFormat() How to pass date ( to text field) by using calendar using PHP & JavaScript 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. We don't expect a wrong entry by user when they select a date from a Calendar. $yy=$arr // third element is year Input date from a calendar $dd=$arr // first element of the array is date If your input format is different then you can change the variables after creating the array by.
$mm=$arr // first element of the array is month $arr=explode("/",$dt) // breaking string to create an array Here we are collecting the user entered date value of a form posted by POST method. 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 is the case where checkdate will return false 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 formatīool checkdate (int month, int day, int year) This function validates the date and returns true if date is correct or false if date is wrong or does not exist. We will try checkdate() function which takes care of leap year checking also. User may select 29th Feb 2005 (which is not a leap year ) or it may select 31st Nov of any year. 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. The combination of entered month date and year by a user has to be a valid date to use in our applications. Many times we have to check the date entered are in correct format or not. Checking a valid date entered by user in PHP