Home >Backend Development >PHP Tutorial >PHP regular matching date and time program code_PHP tutorial

PHP regular matching date and time program code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:00:291267browse

Today I want to replace a database. I need to extract the date and time from the specified string in the database and then convert the timestamp. My field is a character with date. The matching time format is 2012-02-16 or 2012-02-16. 23:59:59 You don’t need to write it when the front is 0. Later I found some test code

and posted the code below:

The code is as follows
 代码如下 复制代码

//匹配时间格式为2012-02-16或2012-02-16 23:59:59前面为0的时候可以不写
$time = "2012-02-16 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";
}
?>

Copy code

 代码如下 复制代码

/^d{4}[-](0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01])(s+(0?[0-9]|[12][0-3]):(0?[0-9]|[1-5][1-9]):(0?[0-9]|[1-5][1-9]))?$/


//The matching time format is 2012-02-16 or 2012-02-16 23:59:59. You don’t need to write it when the front is 0
$time = "2012-02-16 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";
}

?>

Analysis of this regular rule:

The code is as follows

Copy code

/^d{4}[-](0?[1-9]|1[012])[-](0?[1-9]|[12][0-9]|3[01 ])(s+(0?[0-9]|[12][0-3]):(0?[0-9]|[1-5][1-9]):(0?[0- 9]|[1-5][1-9]))?$/
 代码如下 复制代码


<br>
<?php <br />
$str="嘿嘿2010/07/08 12:31:56哈哈1999/12/31 13:21:45";<br />
$str2="嘿嘿2010-07-08 12:31:56哈哈1999-12-31 13:21:45";<br />
//仅匹配日期<br />
preg_match_all("/d{4}/d{2}/d{2}/", $str, $arr);<br />
preg_match_all("/d{4}-d{2}-d{2}/", $str2, $arr2);<br />
//匹配日期与时间<br />
preg_match_all("/d{4}/d{2}/d{2}sd{2}:d{2}:d{2}/", $str, $arr3);<br />
preg_match_all("/d{4}-d{2}-d{2}sd{2}:d{2}:d{2}/", $str2, $arr4);<br />
echo '<font color="red">匹配日期</font><br>';<br>
print_r($arr);<br>
print_r($arr2);<br>
echo '<font color="red">匹配日期和时间</font><br>';<br>
print_r($arr3);<br>
print_r($arr4);<br>
?><br>

/ is the start symbol of the expression and the last / is the end symbol of the expression

^The beginning of the string, starting with ***

$ represents the end of the string, which means it has ended () represents a unit d{4} matches a 4-digit number, (-) matches a "-" sign, (0?[1-9]|1[012]) matches the month, (0?[1-9]|[12][0-9]|3[01]) matches the day, (s+(0?[0-9]|1[0-9]|2[0-3]):(0?[0-9]|[1-5][0-9]): (0?[0-9]|[1-5][0-9]))? The minutes and seconds part of the entire matching time, ? means yes or no. The last step is to convert the timestamp of the successful match
The current conversion result is: 1329407999
Attached is an example I wrote myself
The code is as follows Copy code
<?php <🎜>
$str="Hey 2010/07/08 12:31:56 Haha 1999/12/31 13:21:45";<🎜>
$str2="Hey 2010-07-08 12:31:56 Haha 1999-12-31 13:21:45";<🎜>
//Only match dates<🎜>
preg_match_all("/d{4}/d{2}/d{2}/", $str, $arr);<🎜>
preg_match_all("/d{4}-d{2}-d{2}/", $str2, $arr2);<🎜>
//Match date and time<🎜>
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">match date</font><br>';
print_r($arr);
print_r($arr2);
echo '<font color="red">match date and time</font><br>';
print_r($arr3);
print_r($arr4);
?>
http://www.bkjia.com/PHPjc/631254.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631254.htmlTechArticleToday I want to replace a database and take out the date and time in the specified string in the database and then convert the timestamp. , my field is a character with date and the matching time format is 2012-02-16 or...
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