note 107510 added to function.checkdate
| From: | Anonymous | Date: | Tue, 14 Feb 2012 11:11:00 +0000 |
| Subject: | note 107510 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186122@lists.php.net to get a copy of this message | ||
This is a very good function to check if a date format is valid (with regex).
For example:
2012-2-28 = Valid
2012-2-29 = Valid (leap year)
2012-2-30 = Invalid
2012-1-30 = Valid
2012-1-31 = Valid
2012-2-32 = Invalid
Ensure there are no spaces in the preg_match function:
<?php
function Valid_Date_Format($date)
{
$converted=str_replace('/','-',$date);
if(preg_match("/^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|
(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|
(1[0-2]))-(29|30)))))$/",$converted)===1)
{
return TRUE;
}
return FALSE;
}
?>
I don't know where I got the original code from but I've not found anything better.
----
Server IP: 92.48.74.199
Probable Submitter: 81.136.202.145
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/107510
Del: integrated -- https://master.php.net/note/delete/107510/integrated
Del: useless -- https://master.php.net/note/delete/107510/useless
Del: bad code -- https://master.php.net/note/delete/107510/bad+code
Del: spam -- https://master.php.net/note/delete/107510/spam
Del: non-english -- https://master.php.net/note/delete/107510/non-english
Del: in docs -- https://master.php.net/note/delete/107510/in+docs
Del: other reasons-- https://master.php.net/note/delete/107510
Reject -- https://master.php.net/note/reject/107510
Search -- https://master.php.net/manage/user-notes.php