$str = '';
$isMatched = preg_match('/^\d{4}(\-|\/|.)\d{1,2}\1\d{1,2}$/', $str, $matches);
var_dump($isMatched, $matches);
<?php
//匹配时间格式为2016-02-16或2016-02-16 23:59:59前面为0时可以不写
$time = "2016-12-14 23:59:59";
$patten = "/^\d{4}[\-](0?[1-9]|1[012])[\-](0?[1-9]|[12][0-9]|3[01])(\s+(0?[0-9]|1[0-9]|2[0-3])\:(0?[0-9]|[1-5][0-9])\:(0?[0-9]|[1-5][0-9]))?$/";
if (preg_match($patten, $time)) {
echo $timestro = strtotime($time);
} else {
echo "error";
}
?>
<meta charset='utf-8'>
<pre>
<?php
$str="嘿嘿2010/07/08 12:31:56哈哈1999/12/31 13:21:45";
$str2="嘿嘿2010-07-08 12:31:56哈哈1999-12-31 13:21:45";
//仅匹配日期
preg_match_all("/d{4}/d{2}/d{2}/", $str, $arr);
preg_match_all("/d{4}-d{2}-d{2}/", $str2, $arr2);
//匹配日期与时间
preg_match_all("/d{4}/d{2}/d{2}sd{2}:d{2}:d{2}/", $str, $arr3);
preg_match_all("/d{4}-d{2}-d{2}sd{2}:d{2}:d{2}/", $str2, $arr4);
echo '<font color="red">匹配日期</font><br>';
print_r($arr);
print_r($arr2);
echo '<font color="red">匹配日期和时间</font><br>';
print_r($arr3);
print_r($arr4);
?>
</pre>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有