开发者

Is there a regular expression to check for correct PHP date format entry?

I have an input box that lets users enter a date format in PHP date format, for example: M j, Y

I want to verify on submit that the user actually enters a date that's in PHP date format. I already have the validation script ready, I just need a regular expression or some sort of way to check that the user actually entered a valid PHP date notation.

开发者_如何学编程

Is this even possible?

Thanks for any insights!


The comments on PHP checkdate function are full of examples.

For instance, some thing like this:

<?php
function IsDate( $Str )
{
  $Stamp = strtotime( $Str );
  $Month = date( 'm', $Stamp );
  $Day   = date( 'd', $Stamp );
  $Year  = date( 'Y', $Stamp );

  return checkdate( $Month, $Day, $Year );
}
?>


Check out date_parse.

I do want to point out that this sort of thing is usually better handled by preventing it in the first place using a calendar widget or some such to input the date instead of allowing free form text.


This regex should work for dates of format "2010-02-10" or with slashes and without 0 in the front. Then you can use checkdate function in php to do further validation

  function checkDateFormat($date){

     if (preg_match("#^([0-9]{4})(-|/)([0-9]?[0-9])(-|/)([0-9]?[0-9])$#", $date, $parts)){
            if (checkdate(parts[2],$parts[3],$parts[1])){
                return true;
            else return false;
     }
  }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜