Home > php教程 > php手册 > 日期验证正则表达式

日期验证正则表达式

WBOY
Release: 2016-05-25 16:57:40
Original
1884 people have browsed it
下面提供了三款日期验证正则表达式哦,你可以根据自己的需选择下面三种中期其中一种日期验证方法

 

 代码如下 复制代码

$arr_date = array(
'2004-06-01',
'2005-0x-10',
'12-12-12',
'2000-12-25 00:10:20',
'2007-12-05'
);

for ($i=0; $i{
 $date = $arr_date[$i];
 if(ereg("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs))
 {
     echo "日期字符串$date 符合'yyyy-mm-dd'格式:";
     echo "$regs[1].$regs[2].$regs[3]

";
 }
 else
 {
     echo "日期字符串$date 不符合'yyyy-mm-dd'格式的日期字符串

";
 }
}

//匹配yyyy-mm-dd

 代码如下 复制代码

(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)

 

 

//2:匹配dd/mm/yyyy 或匹配d/m/yyyy

 

 代码如下 复制代码
'/^(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|'
                                .'[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})/((([13578]|1[2])|(0[13578]|1[02])'
                                .'/((0[1-9]|[12][0-9]|3[01])|([1-9]|[12][0-9]|3[1])))|(((0[469]|11)|([469]|11))'
                                .'/((0[1-9]|[12][0-9]|30)|([1-9]|[12][0-9]|30)))|'
                                .'((02|2)/((0[1-9]|[1][0-9]|2[0-8])|([1-9]|[1][0-9]|2[0-8]))'
                                .')))|((([0-9]{2})((0[48]|[2468][048]|[13579][26])|([48]|[2468][048]|[13579][26]))|'
                                .'((((0[48])|([48]))|[2468][048]|[3579][26])00))/(02|2)/29)$/';

 



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template