Php validating 24 hour time format

后端 未结 7 1597
孤城傲影
孤城傲影 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!'
    

提交回复
热议问题