Checking a valid date entered by user in PHPMany 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.
Date Validation using createFromFormat()We can create date objects by using createFromFormat() by entering date and format used.
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
bool checkdate (int month, int day, int year)
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
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.
We can use data entered by user in a query in MySQL table for getting ( selecting ) matching records.
More On Date Object
PHP Date & Time class creating and examples of uses
Creating a date interval object and using with php date object
Adding date interval to date object to create new dates
Difference in date by using diff function and using date object
Add or subtract from date object using modify function
Changing parameters of the date object by using setdate function
updating time for a date object to a new value
Gettign the time zone setting of the server
Setting the default time zone of server
Array of time zones available for setting ( total list )
Getting time in seconds elapsed from Epoch time
Local time of the server or different time zones by using localtime() function