PHP Validate Time Format
<?php
function isTime($time)
{
if(strlen($time) == 5){
return preg_match("#([0-1]{1}[0-9]{1}|[2]{1}[0-3]{1}):[0-5]{1}[0-9]{1}#", $time);
}elseif(strlen($time) == 8){
return preg_match("#([0-1]{1}[0-9]{1}|[2]{1}[0-3]{1}):[0-5]{1}[0-9]{1}:[0-5]{1}[0-9]{1}#", $time);
}
return false;
}
$time1 = '00:12:60';
$time2 = '24:12:14';
$time3 = '15:12:21';
echo "<br>TEST FALSE , isTime('$time1') = " . (isTime($time1) > 0? 'TRUE' : 'FALSE');
echo "<br>TEST FALSE , isTime('$time2') = " . (isTime($time2) > 0? 'TRUE' : 'FALSE');
echo "<br>TEST <b>TRUE</b> , isTime('$time3') = " . (isTime($time3) > 0? '<b>TRUE</b>' : 'FALSE');
?>
ผลลัพธ์
อ้างอิง
http://burnignorance.com/php-programming-tips/how-to-check-time-format-hhmm-in-php/
ความคิดเห็น
แสดงความคิดเห็น