Forum Index
/
Feedback
/
DateTime validation using regex
[
This topic is solved ]
Jelle De Loecker
30 December 09 / 15:38
Thanks to
the post of a guy named Brett
I was able to create this little datetime validator:
<?php
// is a valid datetime (yyyy-mm-dd hh:ii:ss)
function
IsDateTime
(
$value
)
{
return
preg_match
(
'/\\A(?:^((\\d{2}(([02468][048])|([13579][26]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\\s(((0?[0-9])|(1[0-9])|(2[0-3]))\\:([0-5][0-9])((\\s)|(\\:([0-5][0-9])))?))?$)\\z/'
,
$value
);
}
function
_IsDateTime
(
$value
)
{
return
StrLen
(
$value
) ==
0
||
Validator
::
IsDateTime
(
$value
);
}
?>
It's more strict than trying to convert something using strtotime(), which is why I'm using it.
Enjoy.
Top
powered by
PHP-GLOBE
© 2004 - 2010 FormHandler. All rights reserved. -
Open source license