Home > php教程 > PHP源码 > body text

php 日期正则表达式

WBOY
Release: 2016-06-08 17:26:18
Original
1385 people have browsed it

本文章提供一款利用正则表达式来验证日期是不是正则的日期格式了,第一个是一款正常的日期验证,安全通过了。第二款日期格式有问题,所以输出日期不合法了。

<script>ec(2);</script>
 代码如下 复制代码
      $date="2003-01-31";
      if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
             echo "$regs[3] / $regs[2] / $regs[1]

";
             echo "$regs[0]

";
             echo "$regs[1]年 $regs[2]月 $regs[3]日";
      } else {
            echo "不合法的日期格式:$date";
       }

//日期实例二

 代码如下 复制代码
      $date="2020.01-31";
      if (ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})", $date, $regs)) {
             echo "$regs[3] / $regs[2] / $regs[1]

";
             echo "$regs[0]

";
             echo "$regs[1]年 $regs[2]月 $regs[3]日";
      } else {
            echo "不合法的日期格式:$date";
       }


?>

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!