Php > Miscellaneous
PHP Date Validation (including leap year)
PHP Date Validation (including leap year) function GetValidDate($month=1, $day=1, $year=1970) { //** provide a default value and convert the day of the month given. Day //** cannot be less than 1 or greater than 31. $day = min(31, max(1, intval($day))); //** provide a default value and convert the month number given. Month cannot //** be less than 1 or greater than 12. $month = min(12, max(1, intval($month))); //** provide a default value and convert the day of the year number given. $year = max(1970, intval($year)); //** handle compensating for day runover if the number of days selected is //** greater than those allowable for the selected month. switch($month) { //** if FEBRUARY the selected day must be corrected and a leap year must be //** acounted for as well. //** CHANGED JAN 26/2004 - corrected for leap year error. case 2 : if($day > 28) $day = ($year % 4 == 0 && ($year % 100 != 0 || $year % 400 == 0)) ? 29 : 28; break; //** only maximum of thirty days in these months. case 4 : //** APRIL case 6 : //** JUNE case 9 : //** SEPTEMBER case 11 : //** NOVEMBER $day = min(30, $day); break; } //** construct the appropriate date timestamp from the parameters provided. return mktime(0, 0, 0, $month, $day, $year); } ?>
Please choose a date:
January
February
March
April
May
June
July
August
September
October
November
December
for($index=1; $index <= 31; $index++) echo "
$index
"; ?>
for($index=2000; $index <= 2025; $index++) echo "
$index
"; ?>
if(isset($_REQUEST["dodisplay"])) { echo "
You Choose:
", date("F d, Y", GetValidDate($_REQUEST["month"],$_REQUEST["day"],$_REQUEST["year"])), "
(", "month=", $_REQUEST["month"], " day=", $_REQUEST["day"], " year=", $_REQUEST["year"], ")
"; } ?>
Php Codes
Algorithms
Arrays
Authentication
Calendar
Code Snippets
Programs
Content Manage
Contest Related
Cookies
Credit Cards
DBase Related
Databases
Date Time
Directories
E-Mail
Errors
File
File System
Forms
Handling
Graphics
HTML and PHP
Informix
Ingres
InterBase
LDAP
Look and Feel
Miscellaneous
MySQL
Other
PHP Classes
Searching
Navigation
Statistics
Strings
User Manage