Php validating 24 hour time format

后端 未结 7 1545
孤城傲影
孤城傲影 2020-12-30 10:16

I\'m allowing users to pick an hour from 00:00:00 to 23:00:00 and need to check if they submit the right format. Is there a regular expression or p

相关标签:
7条回答
  • 2020-12-30 10:52

    Has it to be a RegEx? You can easily use PHPs strtotime-function to validate dates and times (does also work without a date). strtotime returns false (-1 prior to PHP 5.1) if the given time isn't valid. Don't forget to use the === operand therefore!

    if (strtotime("12:13") === false) { echo("Wrong Time!"); } // Echos nothing
    if (strtotime("19:45") === false) { echo("Wrong Time!"); } // Echos nothing
    if (strtotime("17:62") === false) { echo("Wrong Time!"); } // Echos 'Wrong Time!'
    
    0 讨论(0)
提交回复
热议问题