Created
June 24, 2013 18:37
-
-
Save AkenRoberts/5852358 to your computer and use it in GitHub Desktop.
Help me break this time regex!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
I'm trying to validate times in a PHP application. Some of the formats | |
I want to allow include: | |
- 12am (12 hour clock) | |
- 1:45 pm (space optional) | |
- 23:59 (24 hour clock) | |
- 00:16 | |
- 0:00 | |
- 3 (hour by itself, 24-hour format) | |
- 19 | |
Please help me test and break the regular expression below for this purpose. | |
If you have any other suggestions for a type of time that could be included, | |
I'm all ears! | |
Thanks much. :) | |
Eric "Aken" Roberts, @cryode | |
*/ | |
preg_match('/^([01]?[0-9]|2[0123])(\:[0-5][0-9])?(?:\s?(?<!0|1[3-9])(am|pm))?$/i', $time); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment