Heim  >  Artikel  >  Backend-Entwicklung  >  php日期格式合法性正则验证代码_PHP教程

php日期格式合法性正则验证代码_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:53:56915Durchsuche

php日期格式合法性正则验证代码 本款php正则日期教程,提供了三种验证用户输入的日期是不是正确的日期格式哦,二种是用正则日期验证,一种是用checkdate来验证。

php教程日期格式合法性正则验证代码

本款php正则日期教程,提供了三种验证用户输入的日期是不是正确的日期格式哦,二种是用正则日期验证,一种是用checkdate来验证。
*/
$days = date("y-m-d");

//方法一正则验证日期

$reg="/d{4}-d{2}-d{2}/";
preg_match($reg,$days,$arr);
print_r($arr);


//方法二用cehckdate验证

$k = explode('-',$days);
if( checkdate($k[1],$k[2],$k[0])  )
{
 echo $days,'日期合法';
}
else
{
 echo '不是有效的日期';
}

//方法三简单直观正则验证

if( ereg("(19|20)[0-9]{2}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$",$days))
{
 echo $days,'为有效日期';
}
else
{
 echo

'无效日期';
}

/*
关于 checkdate函数

checkdate() 函数验证一个格里高里日期。

如果指定的值合法,则该函数返回 true,否则返回 false。

日期在下列情况下为合法:
month 介于且包括 1 - 12
day 的值在给定的 month 所应该具有的天数范围之内,闰年已经考虑进去了。
year 介于且包括 1 到 32767


验证格式为 月/日/年

本站原创教程,转载注明来源于www.bKjia.c0m/phper/php.html
*/

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632365.htmlTechArticlephp日期格式合法性正则验证代码本款php正则日期教程,提供了三种验证用户输入的日期是不是正确的日期格式哦,二种是用正则日期验证,...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn