Php validating 24 hour time format

Check it by this function and you can add this function in CodeIgniter helper file of Laravel global helper functions file and call it more times

<?php
if(!function_exists("check_24_timeFormat")) {
    /**
     *
     * This for check time is in  24 time format  
     * 
     * @param string $time  [ $time => time in 24 hours format like 23:00:00 ]
     * @author Arafat Thabet <[email protected]> 
     * @return bool
     */
    function check_24_timeFormat($time){
        if (preg_match("#((0([0-9])|(1[0-9]{1})|(2[0-4])):([0-5])([0-9]):([0-5])([0-9]))#", $time)) {
            return true;
        }
        else
        {
            return false;
        }
    }
}

Here its a final sample there are ready to be used.

$myTime = '23:00:00';

$time = preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', $myTime);

if ( $time == 1 )
{
  // make a error!
}
else
{
  // make a error!
}

Try This

$time="23:00:00";

preg_match('#^([01]?[0-9]|2[0-3]):[0-5][0-9](:[0-5][0-9])?$#', $time);

This matches 24 hour time including seconds

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

If you only want 00 for minutes and seconds, then

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

Tags:

Time

Php

Date