search
Homephp教程php手册php检查是否为一个合法的时间格式正则

php检查是否为一个合法的时间格式正则

Jun 13, 2016 am 10:15 AM
phpunderforseveral kindsuselegitimatetimeFormatexamineregularuserofenterverify

下面总结了几种利用php来验证用户输入的日期是不是正确的日期哦,有需要的朋友可参考参考。

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

例子

 代码如下 复制代码

var_dump(checkdate(12,31,2000));
var_dump(checkdate(2,29,2003));
var_dump(checkdate(2,29,2004));
?>输出:

bool(true)
bool(false)
bool(true)

正则日期

例1

/**

 代码如下 复制代码

* 检查是否为一个合法的时间格式
*
* @param string $time
* @return void
*/
function is_time($time)
{
$pattern = '/[d]{4}-[d]{1,2}-[d]{1,2}s[d]{1,2}:[d]{1,2}:[d]{1,2}/';

return preg_match($pattern, $time);
}

例2

正则验证日期

 代码如下 复制代码

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

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools