note 104852 deleted from function.checkdate by danbrown
| From: | danbrown@php.net | Date: | Wed, 13 Jul 2011 17:09:40 +0000 |
| Subject: | note 104852 deleted from function.checkdate by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-181485@lists.php.net to get a copy of this message | ||
Note Submitter: djpeterson83
----
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.