PHP计算产生负数?

PHP计算产生负数?,php,variables,if-statement,explode,Php,Variables,If Statement,Explode,具有以下值: $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

具有以下值:

$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'");
}
if(!$a[0]==”和!$b[0]==”){
$start=explode(“:”,$a[0]);
$end=explode(“:”,$b[0]);
$total_hours=$end[0]-$start[0]-($end[1]<$start[1]);
mysqli_query($con,“每天更新时钟集_小时数=“$total_小时数”,其中idex=“$data[idex]”,date=“$date”,status='slunch');
}
如果(!$c[0]==”“和!$d[0]==”“){
$start=explode(“:”,$c[0]);
$end=explode(“:”,$d[0]);
$total_hours=$end[0]-$start[0]-($end[1]<$start[1]);
mysqli_query($con,“更新时钟集每日_小时数=“$total_小时数”,其中idex=“$data[idex]”和date=“$date”和status='ework');
}

因为下午1点到9点相当于
1-9
,也就是
-8
。您需要将PM时间转换为24小时制,例如

1pm -> 13
9am -> 9

13 - 9 = 4

您的代码完全按照预期工作。只是你在减去时间,而不是把它转换成24小时制

您可以使用进行转换,如下所示:

$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"));  
这应该可以解决问题

代码板:

$a[0]=“上午4:00:00”;
$b[0]=“上午8:00:00”;
$c[0]=“上午9:00:00”;
$d[0]=“下午1:22:00”;
如果(!$a[0]==”“和!$b[0]==”“){
$start=convert($a[0]);
$end=conver($b[0]);
$total_hours=$end[0]-$start[0]-($end[1]<$start[1]);
mysqli_query($con,“每天更新时钟集_小时数=“$total_小时数”,其中idex=“$data[idex]”,date=“$date”,status='slunch');
}
如果(!$c[0]==”“和!$d[0]==”“){
$start=convert($c[0]);
$end=转换($d[0]);
$total_hours=$end[0]-$start[0]-($end[1]<$start[1]);
mysqli_query($con,“更新时钟集每日_小时数=“$total_小时数”,其中idex=“$data[idex]”和date=“$date”和status='ework');
}
函数转换(小时){
$m=strtolower(substr($hour,-2));//上午或下午
$hr=array_slice(分解(“:”,$hour),0,2);
如果($m=='pm')$hr[0]+=12;
返回$hr;
}
$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;
}