如何获得php总时间?
我已经用PHP创建了工作总时间程序——输入时间为1.30、2.10、1.40,输出时间为4.80(8小时)。但我需要输出时间-5.20(8.40小时)。如何获得php总时间?,php,time,Php,Time,我已经用PHP创建了工作总时间程序——输入时间为1.30、2.10、1.40,输出时间为4.80(8小时)。但我需要输出时间-5.20(8.40小时)。 注:1.30+2.10+1.40=4.80(8小时),但我需要5.20(8.40小时)。请帮帮我…1.30+2.10+1.40是错误的。应该是: ((1*60)+30)+(2*60)+10)+(1*60)+40)=320(分钟) 320分钟=5小时20分钟。您需要分别记录分钟和秒数: $minutes = array(); $seconds =
注:1.30+2.10+1.40=4.80(8小时),但我需要5.20(8.40小时)。请帮帮我…1.30+2.10+1.40是错误的。应该是: ((1*60)+30)+(2*60)+10)+(1*60)+40)=320(分钟)
320分钟=5小时20分钟。您需要分别记录分钟和秒数:
$minutes = array();
$seconds = array();
foreach ($times as $time) {
$parts = explode('.', $time);
$minutes[] = $time[0];
$seconds[] = $time[1];
}
$total_minutes = array_sum($minutes);
$total_seconds = array_sum($seconds);
while ($total_seconds > 60) {
$total_minutes++;
$total_seconds -= 60;
}
echo $total_minutes . ' minutes and ' . $total_seconds . ' seconds';
摘自PHP网站,以备不时之需:
function AddTime ($oldTime, $TimeToAdd) {
$pieces = split(':', $oldTime);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$oldTime=$hours.":".$minutes.":".$seconds;
$pieces = split(':', $TimeToAdd);
$hours=$pieces[0];
$hours=str_replace("00","12",$hours);
$minutes=$pieces[1];
$seconds=$pieces[2];
$str = $minutes." minute ".$seconds." second" ;
$str = "01/01/2000 ".$oldTime." am + ".$hours." hour ".$minutes." minute ".$seconds." second" ;
if (($timestamp = strtotime($str)) === false) {
return false;
} else {
$sum = date('h:i:s', $timestamp);
$pieces = split(':', $sum);
$hours = $pieces[0];
$hours = str_replace("12", "00", $hours);
$minutes = $pieces[1];
$seconds = $pieces[2];
$sum = $hours.":".$minutes.":".$seconds;
return $sum;
}
}
$firstTime = "00:03:12";
$secondTime = "02:04:34";
$sum=AddTime($firstTime, $secondTime);
if($sum != false) {
echo $firstTime." + ".$secondTime." = ".$sum;
} else {
echo "failed";
}
输出:
00:03:12 + 02:04:34 = 02:07:46
对于每个数字(以下表示为$t
),您可以执行以下操作:
// start with $total=0
$hours = floor($t); // 1.10 -> 1 hr
$minutes = ($t - $hours) * 100; // 1.10 -> 10 mins
$total += ($hours * 60) + $minutes;
这将为您提供总分钟数。要分别获取小时/分钟,请执行以下操作:
$total_mins = $total % 60; // 130 -> 10 mins
$total_hours = ($total - $total_mins) / 60; // 130 -> 2 hrs
再加上额外的40分钟。还是有什么你没有告诉我们的?4.80=8小时和5.20=8.40小时?那是什么样的时间测量?爱因斯坦相对论还是什么?4.80和8小时到底是什么样的,我是不是遗漏了什么?我想穆鲁盖什写的4.80代表480分钟(8小时),写的5.20代表520分钟(8小时40分钟)。这段代码在我看来开销很大,但对你来说很有用。