PHP验证字符串和日期 [英] PHP Validate a string and date

查看:335
本文介绍了PHP验证字符串和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中,我有一个字符串,需要检查是否是以下格式:
XXXX-YY-ZZ

In PHP, I have a string that I need to check if it is the following format: XXXX-YY-ZZ

其中x,y和z是整数。因此,该字符串包含3个整数,用2个破折号分隔,具有以下规则:

Where x,y and z are integers. So the string contains 3 integers separated by 2 dashes with the following rules:

xxxx > 2003 
yy >= 0 and yy <= 12 
zz >= 1 zz <= 31

最简单的验证方式是什么?

What would be the easiest way to validate this?

推荐答案

Glavić(也在 php.net ):

function validateDate($date)
{
    $d = DateTime::createFromFormat('Y-m-d', $date);
    return $d && $d->format('Y-m-d') == $date;
}

这篇关于PHP验证字符串和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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