Skip to content

Instantly share code, notes, and snippets.

@AkenRoberts
Created June 24, 2013 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AkenRoberts/5852358 to your computer and use it in GitHub Desktop.
Save AkenRoberts/5852358 to your computer and use it in GitHub Desktop.
Help me break this time regex!
<?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