PHP Regex检查日期为YYYY-MM-DD格式 [英] PHP Regex to check date is in YYYY-MM-DD format

查看:100
本文介绍了PHP Regex检查日期为YYYY-MM-DD格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查最终用户输入的日期是否在YYYY-MM-DD中.正则表达式从来都不是我的强项,我不断为设置的preg_match()返回错误的返回值.

I'm trying to check that dates entered by end users are in the YYYY-MM-DD. Regex has never been my strong point, I keep getting a false return value for the preg_match() I have setup.

所以我假设我把正则表达式弄得一团糟,下面将详细介绍.

So I'm assuming I have made a mess of the regex, detailed below.

$date="2012-09-12";

if (preg_match("^[0-9]{4}-[0-1][0-9]-[0-3][0-9]$",$date))
    {
        return true;
    }else{
        return false;
    }

有什么想法吗?

推荐答案

尝试一下.

$date="2012-09-12";

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

这篇关于PHP Regex检查日期为YYYY-MM-DD格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆