开发者

strtotime & date validation (01/20/2011 vs 01/2011)

开发者 https://www.devze.com 2023-02-06 06:21 出处:网络
01/20/2011 = January 20, 2011 01/2011 = January 2011 Only these two formats should be allowed! i.e. 01/01/01/2011 = ERROR!
01/20/2011 = January 20, 2011

01/2011 = January 2011

Only these two formats should be allowed!

i.e. 01/01/01/2011 = ERROR!

PHP:

$date = '01/20/2011';

//$date = '01/2011';

if(........format test........)//01/20/2011
{
    if(...validation...)
    {
        echo date('F j, Y', strtotime($date));
    }
}
else//01/2011
{
    if(...validation...)
    {
        echo date('F Y', .....);
    }
}

Validation:

IF = 01/20/2011

  • First开发者_JAVA技巧 part should only contain numbers 01-12
  • Second part should only contain numbers 01-31
  • Third part should consist of 4 numbers

IF = 01/2011

  • First part should only contain numbers 01-12
  • Second part should consist of 4 numbers


Would this work:

function custom_checkdate($date){
    $date_parts = explode('/', $date);

    if(count($date_parts) === 3){
        $d = $date_parts[1];
        $m = $date_parts[0];
        $y = $date_parts[2];
    }elseif(count($date_parts) === 2){
        $d = 1;
        $m = $date_parts[0];
        $y = $date_parts[1];
    }else{
        return false;
    }

    if( checkdate($m, $d, $y) ){
        return true;
    }else{
        return false;
    }
}
0

精彩评论

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