次のコードがあります。最後に、サーバーにある結果をコメントに入れました。計算は同じなのに、結果が違う理由を誰かが説明してくれることを願っています。
<?php
date_default_timezone_set('UTC');
function formatHourToTime($input){
if (strpos($input, '.') !== false){
$array = explode(".",$input);
}
elseif (strpos($input, ':') !== false){
$array = explode(":",$input);
}
elseif (strpos($input, ',') !== false){
$array = explode(",",$input);
}
elseif ($input >= '0' & $input < '24'){
$array = array($input);
}
else {
$time = false;
exit();
}
$time = $array[0]*3600+$array[1]*60+$array[2];
return $time;
}
$matin_d = 0; //midnight timestamp 0.00
$matin_f = 10800; //ts de 3h00
$soir_d = 79200; //ts de 22h00
$soir_f = 82799; //ts de 23h59:59
function nightwork($start, $end){
if ($start < $matin_f && $end > $soir_d) $totalheures = ($matin_f - $start)/2 + ($end - $soir_d)/2+100000;
elseif ($start < $matin_f && $end < $matin_f) $totalheures = ($end - $start)/2+200000;
elseif ($start >= $soir_d && $end > $soir_d) $totalheures = ($end - $start)/2+300000;
elseif ($start < $matin_f) $totalheures = ($matin_f-$start)/2+400000;
elseif ($end>$soir_d) $totalheures = ($end-$soir_d)/2+500000;
else $totalheures = 0+600000;
return $totalheures;
}
$start = formatHourToTime('07:39:00')*1;
$end = formatHourToTime('08:00:00')*1;
$shiftnw = nightwork($start, $end);
if($start >= $soir_d && $end > $soir_f) $bool = 'true';
else $bool = 'false';
//même code que la fonction nightwork
if ($start < $matin_f && $end > $soir_d) $totalheures = ($matin_f - $start)/2 + ($end - $soir_d)/2+100000;
elseif ($start < $matin_f && $end < $matin_f) $totalheures = ($end - $start)/2+200000;
elseif ($start >= $soir_d && $end > $soir_d) $totalheures = ($end - $start)/2+300000;
elseif ($start < $matin_f) $totalheures = ($matin_f-$start)/2+400000;
elseif ($end>$soir_d) $totalheures = ($end-$soir_d)/2+500000;
else $totalheures = 0+600000;
echo $start.' '.$end.'<br>';
echo $totalheures. ' ' .$shiftnw;
//$totalheures is calculated following the script
//while $shiftnw is calculated by calling the function having the same lines
// prints :
// 27540 28800
// 600000 300630
?>