SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

Checking a valid date entered by user in PHP


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.

For this PHP has a 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

int 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

$m=”11”;
$d=”31”;
$y=”05”;
If(!checkdate($m,$d,$y)){
echo “invalid date”;
}else {
echo “Entry date is correct “;
}

If we are asking the user to enter date in a text field then we have to break the entered date value by using split 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";
$arr=split("/",$dt); // splitting the 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";
}

We can use data entered by user in a query in MySQL table for getting ( selecting ) matching records.
Further readings
PHP Date & Time functions
PHP Date Time
Validating Date: Checking if date exist
PHP Date Today
PHP yesterday and previous 7 days from today
PHP Date Format
Time stamp generator
Date selection drop down list box
mktime function to generate time stamp
Difference in Two dates by using mktime function
Finding month, weekday, day of the year etc for any date value
Breaking the date string to get month date and year using array













Mat09-02-2009
how to compare the data entered by the user to the data in the database?
smo10-02-2009
It can be used in a sql query. A link is added now on this at the end of the tutorial above.
jisha28-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?
smo29-06-2009
In SQL section you can read this article on how to get record between two dates.
tonier04-09-2009
Just remember this, in SQL, valid format is 'Y-m-d' and day and month must be in 2 digits char.
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Sections
PHP
JavaScript
ASP
HTML
SQL
Photoshop
Articles SEO
PHP Tutorials
Date & Time
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.