简体   繁体   English

Preg_match(yyyy-mm-dd hh:mm:ss) - 只有这种格式

[英]Preg_match (yyyy-mm-dd hh:mm:ss) - only this format

function Check_Date_Time($date_time)
{
 if (preg_match("/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/", $date_time))
   {
     return true;
   }
     else 
     {
     return false;
     }
}   

if(Check_Date_Time($date)){
    //echo 'correct';
}else{
    die();
}

If I set the date as such 如果我这样设定日期

["DueDate3"]=> string(24) "2014-10-17 21:10:1751111" 

the validator let its trough, I got no ideas why :/ If it is a properly used date format it returns true. 验证器让它低谷,我不明白为什么:/如果它是一个正确使用的日期格式,它返回true。 I don't really can use other formats - only this is acceptable 我真的不能使用其他格式 - 只有这是可以接受的

添加start ^并结束$ anchors到你的正则表达式。

if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/", $date_time))
 ^  Start of string or line  
\A  Start of string  
 $  End of string or line  
\Z  End of string  

Try to put some of these anchor characters in the code: 尝试在代码中放入一些锚点字符:

preg_match("/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/", $date_time)

You can find more basic characters for regexp here . 你可以在这里找到regexp的更多基本字符。

添加锚到你的正则表达式:

 if (preg_match("/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})$/", $date_time))

try this regex expression 试试这个正则表达式

$date_time = "2019-02-21 11:25:34";
if (preg_match("/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/", $date_time)

this might help you out. 这可能会帮助你。 Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM