douluan5523 2012-09-07 17:34
浏览 32
已采纳

PHP preg_match与正则表达式[重复]

This question already has an answer here:

function validateDate( $date )
{ 
    echo $date;
    //2012-08-24 20:30:00
    if(preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2}) ([1-2]{1})([0-9]{1}):([0-5]{1})([0-9]{1}):([0-5]{1})([0-9]{1})$/', $date) >= 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

This always returns false. I used an only tool to build the regular expression and it was working fine there. Trouble started when I added the "/" to the regex. Somehow PHP seems to require these but I dont know why and I dont know why it breaks my regex.

It should return TRUE for sth. like "2012-08-24 20:30:00" and FALSE for "asdf2012-08-24 20:30:00asdf" or anything thats not acording to my regex

Thanks in advance!

-----------------------EDIT Thanks for all your answers!

As some users pointed out my function returns true for the sample date "2012-08-24 20:30:00". However it does that only if i manually set $date='2012-08-24 20:30:00'. If i call the function elsewhere in my code with the exact same string it returns false. Does anyone know why?

-----------------------EDIT2

Yea sorry for wasting your time, it was in fact some whitespace that was added to the string. using trim() on my date before calling my function gives the correct result now.

thanks everyone!

</div>
  • 写回答

4条回答 默认 最新

  • dourui7186 2012-09-07 17:50
    关注

    Why use regex? Use DateTime class.

    function validateDate($date, $format = 'Y-m-d H:i:s')
    {
        $d = DateTime::createFromFormat($format, $date);
        return $d && $d->format($format) == $date;
    }
    

    You can use this function for all kind of date/time validations. Examples:

    var_dump(validateDate('2012-02-28 12:12:12')); # true
    var_dump(validateDate('2012-02-30 12:12:12')); # false
    var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
    var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
    var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
    var_dump(validateDate('14:50', 'H:i')); # true
    var_dump(validateDate('14:77', 'H:i')); # false
    var_dump(validateDate(14, 'H')); # true
    var_dump(validateDate('14', 'H')); # true
    
    var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
    # or
    var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true
    
    var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
    # or
    var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
    var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
    # ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 在误装Windows server2019 后如何利用Windows.old恢复?
  • ¥20 代码实现状态连接包过滤防火墙的设计与实现
  • ¥15 vscode的红色箭头爆红和has no default export报错
  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 eNSP中基于默认路由及浮动路由的公司与分部互联和校园网综合项目
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错