I want to validate Date in my php application. User should select a date by clicking on calander icon or typinig. I want to check whether the date is in "2008-07-01" format. How can i do that? Could some one help me?
Date validation
Collapse
X
-
-
try this:
Code:<?php $date_in = '2008-07-01'; list($year, $month, $day) = explode('-', $date_in); if (checkdate($month, $day, $year)) { echo 'date valid.'; } else { echo 'date invalid'; }Comment
-
nice, but regex would be faster.Originally posted by nashruddintry this:
Code:<?php $date_in = '2008-07-01'; list($year, $month, $day) = explode('-', $date_in); if (checkdate($month, $day, $year)) { echo 'date valid.'; } else { echo 'date invalid'; }
Here's one that checks for February dates range 0-29:
if you don't care about February 30 / 31 date checking try this:Code:preg_match("/^(19|20)?[0-9]{2}[-/ \\.]((0?[13-9]|1[012])[-/ \\.](0?[1-9]|[12][0-9]|3[01])|(0?2[-/ \\.][012]?[0-9]))$/",$variable);
please let me know if there's any problems in the first one, I hand coded it myself.Code:preg_match("/^(19|20)?[0-9]{2}[- /.](0?[1-9]|1[012])[- /.](0?[1-9]|[12][0-9]|3[01])$/",$variable);
-DanComment
Comment