具有下列值:
$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小时值,但第二个语句没有。是因为上午/下午的变化还是什么原因?
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'");
}发布于 2013-07-17 01:41:29
因为下午1点到9点相当于-8的1 - 9。您需要将PM时间转换为24小时制,例如
1pm -> 13
9am -> 9
13 - 9 = 4发布于 2013-07-17 02:03:33
您的代码完全按照预期的方式工作。只是你在减去时间,而不是把它转换成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")); 这应该可以解决这个问题。
代码板:
发布于 2013-07-17 02:04:45
$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;
}https://stackoverflow.com/questions/17683406
复制相似问题