0
我有以下代碼。最後,我將評論結果發送給服務器。希望有人能夠解釋爲什麼結果不同,儘管計算結果是相同的。php,相同的數據,相同的計算,不同的結果
<?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
?>
是否有任何理由不能使用核心的DateTime類來實現你想要做的任何事情?它幾乎可以實現任何格式。 http://php.net/manual/en/class.datetime.php – DeaconDesperado
是的,我已經試圖理解那門課沒有成功。在日照時間較長的幾個月內再試一次;) – user1964936