Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8171339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:25:56+00:00 2026-06-06T21:25:56+00:00

I want to validate a time with a regex. I created the following expression

  • 0

I want to validate a time with a regex. I created the following expression :

'#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#'

Here is the problem:

<?php
var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:25'));
// Returns 1 (OK)

var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '25:25'));
// Returns 0 (OK)

var_dump(preg_match('#^([01][0-9])|(2[0-4])(:[0-5][0-9]){1,2}$#', '14:2555'));
// Returns 1 (instead of 0 as I would like to get)
?>

Does anybody know what is wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:25:57+00:00Added an answer on June 6, 2026 at 9:25 pm

    Time in 24-Hour format regular expression pattern :

    ([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?
    

    The 24-hour clock format is start from 0-23 or 00-23 then a semi colon (:) and follow by 00-59 then (optionally a semi colon (:) and follow by 00-59).

    Description :

    (                  # start of group #1
      [01]?[0-9]       # start with 0-9,1-9,00-09,10-19
      |                # or
      2[0-3]           # start with 20-23
    )                  # end of group #1
    :                  # follow by a semi colon (:)
    [0-5][0-9]         # follow by 0..5 and 0..9, which means 00 to 59
    (                  # start of group #2
      :                # follow by a semi colon (:)
      [0-5][0-9]       # follow by 0..5 and 0..9, which means 00 to 59
    )                  # end of group #2
    ?                  # optional third part
    

    Matching time formats :

    01:00, 02:00, 13:00,
     1:00,  2:00, 13:01,
    23:59, 15:00,
    00:00,  0:00,
    14:34:43, 01:00:00
    

    Not matching time formats :

     24:00             # hour is out of range [0-23]
     12:60             # minute is out of range [00-59]
      0:0              # invalid format for minute, at least 2 digits
     13:1              # invalid format for minute, at least 2 digits
      0:00:0           # invalid format for seconds, at least 2 digits
    101:00             # hour is out of range [0-23]
    

    Example :

    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:25'));    // OK
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:25'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '25:30'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:2555'));  // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:65'));    // KO
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:59'));    // OK
    var_dump(preg_match('#^[01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', '14:34:43')); // OK
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following regex to validate date in format dd/mm/yyyy in php: preg_match(/([0-9]{2})\/([0-9]{2})\/([0-9]{4})/,
I want to validate mathematical expressions using regular expression. The mathematical expression can be
I want to validate below data using regex and python. Below is the dump
I'm new to java script. I want to validate wether the time selected from
I am trying to use a VBA regular expression to validate a time range
I want to validate file types in server using php. Now at the moment
I want to validate Checkbox in javascript, checkboxes is generating dynamically by PHP and
I want to validate just one field of a form. Here is my model:
I need to validate a date/time field on a webpage but want it to
I want to validate a phone number input. The phone numbers should be mix

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.