Home > Backend Development > PHP Tutorial > PHP regular verification date and time format example code_PHP tutorial

PHP regular verification date and time format example code_PHP tutorial

WBOY
Release: 2016-07-13 17:16:04
Original
1839 people have browsed it

Using PHP to verify the date format, we can use the checkdate() function and regular expressions to verify. Below, I will introduce to you some of my processes for verifying the date format one by one, and finally came up with the simplest method.

1. Use regular expressions to verify date and time format
Mainly use PHP functions such as ereg, preg_match and so on.
There is a very simple one,

The code is as follows
 代码如下 复制代码
$dateTime=”2010-6-4 00:00:00″;
if(preg_match(“/^d{4}-d{2}-d{2} d{2}:d{2}:d{2}$/s”,$dateTime))
{
echo “Yes”;
}else{
echo “No”;
}
Copy code

$dateTime="2010-6-4 00 :00:00″;
 代码如下 复制代码

regex = “^((d{2}(([02468][048])|([13579][26]))[-/s]?((((0?[13578]
)|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[4
69])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/
s]?((0?[1-9])|([1-2][0-9])))))|(d{2}(([02468][1235679])|([1
3579][01345789]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((
0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s]?((
0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|(1[0-9]
)|(2[0-8])))))) “; //日期部分
regex += “(s(((0?[0-9])|([1-2][0-3])):([0-5]?[0-9])((s)|(:([0-5]?[0-9])))))?$”; //时间部分

if(preg_match(“/^d{4}-d{2}-d{2} d{2}:d{2}:d{2}$/s”,$dateTime))

{
echo “Yes”;

}else{

echo “No”;
}


However, this regularity only verified the numbers and did not verify the boundary values. It was not comprehensive, so someone wrote this regularity

0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[-/s] ?(( 0?[1-9])|([1-2][0-9])|(30)))|(0?2[-/s]?((0?[1-9])|( 1[0-9]
The code is as follows

Copy code
 代码如下 复制代码
preg_match(“/^d{4}-d{2}-d{2} d{2}:d{2}:d{2}$/s”,$dateTime)

regex = “^((d{2}(([02468][048])|([13579][26]))[-/s]?((((0?[13578]
)|(1[02]))[-/s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(( (0?[4
69])|(11))[-/s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[- /
s]?((0?[1-9])|([1-2][0-9])))))|(d{2}(([02468][1235679])|([1

3579][01345789]))[-/s]?((((0?[13578])|(1[02]))[-/s]?((
)|(2[0-8])))))) “; //Date part regex += “(s(((0?[0-9])|([1-2][0-3])):([0-5]?[0-9])((s)| (:([0-5]?[0-9])))))?$”; //Time part

This is simply the work of God. So I had to give up using regular expressions to verify the legality of the time format. 2. Disassemble the time string and verify the date and time format separately. Mainly used PHP functions such as checkdate. This method is very good and the verification is accurate, but it is troublesome to write the code. You need to disassemble the date and time separately. Use checkdate($k[1],$k[2],$k[0]) to verify the date, and the time It needs to be disassembled and inspected one by one. In fact, it can be used by combining the above two methods, and the operation is much more convenient. 1. First use regex to verify whether it is in the format of "2011-11-07 12:30:55".
Use
That’s it
2. Then use the strtotime() function to judge and verify, and just pass in the date string. The strtotime() function returns the UNIX timestamp corresponding to the specified date and time string by default. One feature of the strtotime() function is that it will return false if the format of the incoming date string is wrong, and it supports various date formats, which is very convenient. http://www.bkjia.com/PHPjc/628719.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628719.htmlTechArticleUsing php to verify the date format, we can use the checkdate() function and regular expressions to verify, I will do it one by one below I introduced to you some of my process of verifying the date format, and finally came to...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template