具有以下值:
$a[0] = "4:00:00 am"
$b[0] = "8:00:00 am"
$c[0] = "9:00:00 am"
$d[0] = "1:22:00 pm"
为什么第二个if语句会将-8值写入数据库?第一个if语句产生适当的4小时值,但不是第二个。是因为am / pm变化还是什么?
if (!$a[0]=="" AND !$b[0]=="") {
$start = explode(':', $a[0]);
$end = explode(':', $b[0]);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
mysqli_query($con,"UPDATE timeclock SET daily_hours='$total_hours' WHERE idex='$data[idex]' AND date='$date' AND status='slunch'");
}
if (!$c[0]=="" AND !$d[0]=="") {
$start = explode(':', $c[0]);
$end = explode(':', $d[0]);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
mysqli_query($con,"UPDATE timeclock SET daily_hours='$total_hours' WHERE idex='$data[idex]' AND date='$date' AND status='ework'");
}
答案 0 :(得分:1)
因为下午1点到9点等同于1 - 9
,即-8
。您需要将PM时间转换为24小时制,例如
1pm -> 13
9am -> 9
13 - 9 = 4
答案 1 :(得分:0)
您的代码完全按预期工作。只是你在不将其转换为24小时格式的情况下减去时间。
您可以使用date()
进行转换,如下所示:
$time_in_24 = date("H:i", strtotime("1:22:00 pm")); // 13:22
将您的代码更改为:
$a[0] = date("H:i", strtotime("4:00:00 am"));
$b[0] = date("H:i", strtotime("8:00:00 am"));
$c[0] = date("H:i", strtotime("9:00:00 am"));
$d[0] = date("H:i", strtotime("1:22:00 pm"));
那应该解决它。
答案 2 :(得分:0)
$a[0] = "4:00:00 am";
$b[0] = "8:00:00 am";
$c[0] = "9:00:00 am";
$d[0] = "1:22:00 pm";
if (!$a[0]=="" AND !$b[0]=="") {
$start = convert($a[0]);
$end = conver($b[0]);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
mysqli_query($con,"UPDATE timeclock SET daily_hours='$total_hours' WHERE idex='$data[idex]' AND date='$date' AND status='slunch'");
}
if (!$c[0]=="" AND !$d[0]=="") {
$start = convert($c[0]);
$end = convert($d[0]);
$total_hours = $end[0] - $start[0] - ($end[1] < $start[1]);
mysqli_query($con,"UPDATE timeclock SET daily_hours='$total_hours' WHERE idex='$data[idex]' AND date='$date' AND status='ework'");
}
function convert($hour){
$m = strtolower(substr($hour, -2));//am or pm
$hr = array_slice(explode(':', $hour), 0, 2);
if ($m == 'pm') $hr[0] += 12;
return $hr;
}