Jump to content

Recommended Posts

I took what Ken2k7 posted and fixed it. This is tested:

 

function validTime($time)
{
    return preg_match("/^((0?[0-9])|(1[0-9])|(2[0-3]))(:[0-5][0-9]){2}$/", $time);
}

 

To be valid the time must follow the following parameters:

 

The beginning must be one of the following

(0-9) (any single digit number)

0(0-9) (0 followed by any single digit number)

1(0-9) (1 followed by any single digit number)

2(0-3) (2 followed by 0, 1, 2 or 3)

 

There must then be a colon followed by

(0-5)(0-9) (first digit a 0, 1, 2, 3, 4, or 5, second digit any number)

 

Then another colon followed by

(0-5)(0-9) (first digit a 0, 1, 2, 3, 4, or 5, second digit any number)

 

One problem with the above is that it accepts 00:00:00 but not 24:00:00. Some people prefer one over the other. If you need to support 24:00:00, then change the regex to this:

 

/^(((0?[0-9])|(1[0-9])|(2[0-3]))(:[0-5][0-9]){2})|(24:00:00)$/

Link to comment
https://forums.phpfreaks.com/topic/198088-help-with-regex/#findComment-1039571
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.