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 语句会导致将 -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'");
}
4

3 回答 3

1

因为下午 1 点到上午 9 点相当于1 - 9, 即-8. 您需要将您的下午时间转换为 24 小时制,例如

1pm -> 13
9am -> 9

13 - 9 = 4
于 2013-07-16T17:41:28.510 回答
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"));  

那应该解决它。

键盘:http ://codepad.org/jsltDXK0

于 2013-07-16T18:03:32.970 回答
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;
}
于 2013-07-16T18:04:44.937 回答