ソースコード
<?php
$data[]='0:00';
$data[]='00:00';
$data[]='1:1';
$data[]='0:59';
$data[]='1:00';
$data[]='9:59';
$data[]='10:10';
$data[]='13:10';
$data[]='23:59';
$data[]='08:08';
$data[]='';
$data[]=' ';
$data[]=null;
$data[]=' 8:5 ';
$data[]=' 8 : 12 ';
$data[]='12:12:12';
$data[]='1234';
$data[]='abc';
$data[]='1:5';
$data[]='2012/12/12 5:5';
$data[]='9';
$data[]='24:00';
$data[]='0:60';
$data[]='0.1:0.2';
foreach($data as $i => $v){
$rs=is_time_hi($v);
if($rs==true){
echo "<span class='rs1'>{$i}</span>:<span class='rs2'>{$v}</span>:<span class='rs3'>true</span><br>";
}else{
echo "<span class='rs1'>{$i}</span>:<span class='rs2'>{$v}</span>:<span class='rs3_false'>false</span><br>";
}
}
//時分チェック(h:i形式)
function is_time_hi($v){
$v=trim($v);
if(empty($v)){
return true;
}
$ary=explode(':',$v);
if(count($ary) != 2){
return false;
}
$h=trim($ary[0]);
$m=trim($ary[1]);
if(!preg_match('/^[0-9]+$/', $h)){
return false;
}
if(!preg_match('/^[0-9]+$/', $m)){
return false;
}
if($h < 0 || $h > 23){
return false;
}
if($m < 0 || $m > 59){
return false;
}
return true;
}
?>
実行結果
0:
0:00:
true1:
00:00:
true2:
1:1:
true3:
0:59:
true4:
1:00:
true5:
9:59:
true6:
10:10:
true7:
13:10:
true8:
23:59:
true9:
08:08:
true10:
:
true11:
:
true12:
:
true13:
8:5 :
true14:
8 : 12 :
true15:
12:12:12:
false16:
1234:
false17:
abc:
false18:
1:5:
false19:
2012/12/12 5:5:
false20:
9:
false21:
24:00:
false22:
0:60:
false23:
0.1:0.2:
false