note 104852 added to function.checkdate
| From: | djpeterson83 at osu1 dot php dot net | Date: | Mon, 11 Jul 2011 20:42:38 +0000 |
| Subject: | note 104852 added to function.checkdate | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-181422@lists.php.net to get a copy of this message | ||
Many of the examples below display attempt to use checkdate to test a hand-entered date for
validity. I don't think checkdate was meant for that. My experience has shown the following
simple one liner to be the most effective:
<?php
$isvalid = strtotime($hand_entered_text) !== false;
?>
I've created a test unit for this expression and thrown all sorts of weird stuff at it
including dates with XML and found that it only returns true for anything that would fit exactly a
date. The following would then be valid:
2011-07-11 15:27:41
2011-07-11
15:27:41
12/12/2001
December 31st, 2001
and these are not:
12/32/2001
random test
null
0
56445
With some quick sub-string checks (for colons or slashes and dashes) you could very easily write a
validator based on this that only accepts dates or times but not both.
----
Server IP: 69.147.83.197
Probable Submitter: 173.58.9.188
----
Manual Page -- http://www.php.net/manual/en/function.checkdate.php
Edit -- https://master.php.net/note/edit/104852
Del: integrated -- https://master.php.net/note/delete/104852/integrated
Del: useless -- https://master.php.net/note/delete/104852/useless
Del: bad code -- https://master.php.net/note/delete/104852/bad+code
Del: spam -- https://master.php.net/note/delete/104852/spam
Del: non-english -- https://master.php.net/note/delete/104852/non-english
Del: in docs -- https://master.php.net/note/delete/104852/in+docs
Del: other reasons-- https://master.php.net/note/delete/104852
Reject -- https://master.php.net/note/reject/104852
Search -- https://master.php.net/manage/user-notes.php