Tuesday, November 22, 2011

Validate the MySQL Time in PHP

/*
 * Validate the MySQL Time
 *
 * @author   Junaid Atari <mj.atari@gmail.com>
 * @version  1.0
 * @param    string  $str   MySQL Time to check
 * @return   bool    TRUE on valid | FALSE
*/
function isValidMySQLTime $str )
{
    
/* PCRE Pattern written by Junaid Atari */
    
return !preg_match '/^(?<h>0\d|1\d|2[0-3])\:(?<i>[0-5]\d)\:'.
                         
'(?<s>[0-5]\d)$/'$str )
            ? 
false
            
true;
}


/*
 +-----------+
 |  Example  |
 +-----------+
*/
var_dump isValidMySQLTime '11:25:26' ) );

/*
 +-----------+
 |  Output  |
 +-----------+
*/
# Output: True 

0 comments:

Post a Comment