减去一定数量的时间来增加次数
Substract an amount of time to an addition of times
我必须在 php 中将 32:30:00(字符串)减去 95:05:00(字符串):
95:05:00 - 32:30:00
他们来自时间的增加。
我找不到任何有效的代码,因为 strtotime 不接受超过 24 的值。
请帮帮我,谢谢。
例如,我试过这个:
$time1 = strtotime('32:30:00');
$time2 = strtotime('95:05:00');
$difference = round(abs($time2 - $time1) / 3600,2);
echo 'différence : '.$difference;
它returns 0
应该return类似于62:35:00
你知道我是否可以用 moment.js 或 php 库来做吗?
strtotime
不处理持续时间,只处理有效的时间戳。您可以通过将时间戳分解为小时、分钟和秒来分解时间,从而自行处理。然后您可以将它们转换为总秒数。
<?php
$time1 = '95:05:00';
$time2 = '32:30:00';
function timeToSecs($time) {
list($h, $m, $s) = explode(':', $time);
$sec = (int) $s;
$sec += $h * 3600;
$sec += $m * 60;
return $sec;
}
$t1 = timeToSecs($time1);
$t2 = timeToSecs($time2);
$tdiff = $t1 - $t2;
echo "Difference: $tdiff seconds";
然后我们可以将其转换回小时分秒:
$start = new \DateTime("@0");
$end = new \DateTime("@$tdiff");
$interval = $end->diff($start);
$time = sprintf(
'%d:%02d:%02d',
($interval->d * 24) + $interval->h,
$interval->i,
$interval->s
);
echo $time; // 62:35:00
获得秒数差异的一种方法是使用 mktime
。
$diff = mktime(...explode(':', $time1)) - mktime(...explode(':', $time2));
有多种方法可以转换回您想要的字符串格式。我打算建议使用 sprintf
,但其他答案已经显示了,所以我不会打扰。
一般来说,当你处理时间间隔时,我认为在几秒钟内完成所有计算然后在需要输出结果时格式化结果会更容易,这样你就可以避免需要做这种事情东西。
这是一个替代方案。
$time1 = '32:30:00';
$time2 = '95:05:00';
function timeDiff($time1, $time2)
{
$time1 = explode(':', $time1);
$time2 = explode(':', $time2);
// Make sure $time2 is always the bigger time
if ($time1[0] > $time2[0]) {
$temp = $time1;
$time1 = $time2;
$time2 = $temp;
}
// Work out the difference in each of the hours, minutes and seconds
$h = abs($time2[0] - $time1[0]);
$m = abs($time2[1] - $time1[1]);
$s = abs($time2[2] - $time1[2]);
// Ensure that it doesn't say "60", since that is not convention.
$m = $m == 60 ? 0 : $m;
$s = $s == 60 ? 0 : $s;
// If minutes 'overflows', then we need to remedy that
if ($time2[1] < $time1[1]) {
$h -= $h == 0 ? $h : 1;
$m = 60 - $m;
}
// Fix for seconds
if ($time2[2] < $time1[2]) {
$m -= $m == 0 ? -59 : 1;
$s = 60 - $s;
}
// Zero pad the string to two places.
$h = substr('00'.$h, -2);
$m = substr('00'.$m, -2);
$s = substr('00'.$s, -2);
return "{$h}:{$m}:{$s}";
}
echo timeDiff($time1, $time2);
我简单的分别算出每个时分秒的差值
我对代码进行了相应的注释,以便更深入地了解每个阶段发生的事情。
我必须在 php 中将 32:30:00(字符串)减去 95:05:00(字符串):
95:05:00 - 32:30:00
他们来自时间的增加。
我找不到任何有效的代码,因为 strtotime 不接受超过 24 的值。
请帮帮我,谢谢。
例如,我试过这个:
$time1 = strtotime('32:30:00');
$time2 = strtotime('95:05:00');
$difference = round(abs($time2 - $time1) / 3600,2);
echo 'différence : '.$difference;
它returns 0
应该return类似于62:35:00
你知道我是否可以用 moment.js 或 php 库来做吗?
strtotime
不处理持续时间,只处理有效的时间戳。您可以通过将时间戳分解为小时、分钟和秒来分解时间,从而自行处理。然后您可以将它们转换为总秒数。
<?php
$time1 = '95:05:00';
$time2 = '32:30:00';
function timeToSecs($time) {
list($h, $m, $s) = explode(':', $time);
$sec = (int) $s;
$sec += $h * 3600;
$sec += $m * 60;
return $sec;
}
$t1 = timeToSecs($time1);
$t2 = timeToSecs($time2);
$tdiff = $t1 - $t2;
echo "Difference: $tdiff seconds";
然后我们可以将其转换回小时分秒:
$start = new \DateTime("@0");
$end = new \DateTime("@$tdiff");
$interval = $end->diff($start);
$time = sprintf(
'%d:%02d:%02d',
($interval->d * 24) + $interval->h,
$interval->i,
$interval->s
);
echo $time; // 62:35:00
获得秒数差异的一种方法是使用 mktime
。
$diff = mktime(...explode(':', $time1)) - mktime(...explode(':', $time2));
有多种方法可以转换回您想要的字符串格式。我打算建议使用 sprintf
,但其他答案已经显示了,所以我不会打扰。
一般来说,当你处理时间间隔时,我认为在几秒钟内完成所有计算然后在需要输出结果时格式化结果会更容易,这样你就可以避免需要做这种事情东西。
这是一个替代方案。
$time1 = '32:30:00';
$time2 = '95:05:00';
function timeDiff($time1, $time2)
{
$time1 = explode(':', $time1);
$time2 = explode(':', $time2);
// Make sure $time2 is always the bigger time
if ($time1[0] > $time2[0]) {
$temp = $time1;
$time1 = $time2;
$time2 = $temp;
}
// Work out the difference in each of the hours, minutes and seconds
$h = abs($time2[0] - $time1[0]);
$m = abs($time2[1] - $time1[1]);
$s = abs($time2[2] - $time1[2]);
// Ensure that it doesn't say "60", since that is not convention.
$m = $m == 60 ? 0 : $m;
$s = $s == 60 ? 0 : $s;
// If minutes 'overflows', then we need to remedy that
if ($time2[1] < $time1[1]) {
$h -= $h == 0 ? $h : 1;
$m = 60 - $m;
}
// Fix for seconds
if ($time2[2] < $time1[2]) {
$m -= $m == 0 ? -59 : 1;
$s = 60 - $s;
}
// Zero pad the string to two places.
$h = substr('00'.$h, -2);
$m = substr('00'.$m, -2);
$s = substr('00'.$s, -2);
return "{$h}:{$m}:{$s}";
}
echo timeDiff($time1, $time2);
我简单的分别算出每个时分秒的差值
我对代码进行了相应的注释,以便更深入地了解每个阶段发生的事情。