php 中超过 24 小时的字符串
string to time over 24 hour in php
语法错误你的变量名是 $hour 但你最后使用了 $hours。
$time1 = strtotime("02:40:00");
$time2 = strtotime("34:20:00");
$diff = $time2 - $time1;
$hour = floor($diff / (60 * 60));
$minute = $diff - $hour * (60 * 60);
//$final = $hours .":".floor( $minute / 60 );
$final = $hour .":".floor( $minute / 60 );
echo $final;
您可以使用如下自定义函数:-
<?php
$actual_time = "34:20";
$time_to_reduce = "2:40";
function timeSubtactionFirstTime($actual_time ,$time_to_reduce){
$actual_time_array = explode(":",$actual_time);
$time_to_reduce = explode(":",$time_to_reduce);
$final_result = [];
if($actual_time_array[1] < $time_to_reduce[1]){
$actual_time_array[0] = $actual_time_array[0]-1;
$final_result[] = $actual_time_array[1]+60-$time_to_reduce[1];
}else{
$final_result[] = $actual_time_array[1]-$time_to_reduce[1];
}
$final_result[] = $actual_time_array[0]-$time_to_reduce[0];
return implode(":", array_reverse($final_result));
}
echo timeSubtactionFirstTime($actual_time ,$time_to_reduce);
注意:- 我在这里不考虑秒数。为此需要一点逻辑
将您的时间字符串转换为 DateInteval 字符串
$time1 = "02:40:00";
$time2 = "34:20:00";
list($h, $m, $s) = explode(':', $time1);
$time1 = new DateInterval("PT{$h}H{$m}M{$s}S");
list($h, $m, $s) = explode(':', $time2);
$time2 = new DateInterval("PT{$h}H{$m}M{$s}S");
$T1 = new DateTime('midnight');
$T2 = clone $T1;
$T1->add($time1)->sub($time2);
$diff = $T2->diff($T1);
echo ($diff->invert ? '-' : '') . ($diff->d*24 + $diff->h) . ':' .$diff->i;
我有自己的做法:
<?php
/**
* Calculate the number of hours and minutes between two times.
* @param string $start The start time
* @param string $end The end time
* @return string The difference in hours and minutes
*/
function timeDiff( $start, $end )
{
// Since the end time may be more than 23 hours ...
$seg = (int) substr( $end, 0, 2 );
// Add seconds to time2 ?
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
// Subtract hours from time 2 if necessary
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
// Compute the diff in seconds
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
// How many hours and minutes were there ?
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
// Pad the hours and minutes
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute;
}
// Simple test
$start = "02:40:00";
$end = "34:20:00";
echo timeDiff( $start, $end );
而如果你也想计算秒数,那就稍微修改一下:
<?php
/**
* Calculate the number of hours, minutes,
* and seconds between two times.
* @param string $start The start time
* @param string $end The end time
* @return string The difference in hours:minutes:seconds
*/
function timeDiff( $start, $end )
{
// Since the end time may be more than 23 hours ...
$seg = (int) substr( $end, 0, 2 );
// Add seconds to time2 ?
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
// Subtract hours from time 2 if necessary
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
// Compute the diff in seconds
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
// How many hours and minutes were there ?
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
$second = $diff - ( $hour * 60 * 60 ) - ( $minute * 60 );
// Pad the hours, minutes, and seconds
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
$second = str_pad( $second, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute . ':' . $second;
}
// Simple test
$start = "02:40:00";
$end = "34:20:45";
echo timeDiff( $start, $end );
语法错误你的变量名是 $hour 但你最后使用了 $hours。
$time1 = strtotime("02:40:00");
$time2 = strtotime("34:20:00");
$diff = $time2 - $time1;
$hour = floor($diff / (60 * 60));
$minute = $diff - $hour * (60 * 60);
//$final = $hours .":".floor( $minute / 60 );
$final = $hour .":".floor( $minute / 60 );
echo $final;
您可以使用如下自定义函数:-
<?php
$actual_time = "34:20";
$time_to_reduce = "2:40";
function timeSubtactionFirstTime($actual_time ,$time_to_reduce){
$actual_time_array = explode(":",$actual_time);
$time_to_reduce = explode(":",$time_to_reduce);
$final_result = [];
if($actual_time_array[1] < $time_to_reduce[1]){
$actual_time_array[0] = $actual_time_array[0]-1;
$final_result[] = $actual_time_array[1]+60-$time_to_reduce[1];
}else{
$final_result[] = $actual_time_array[1]-$time_to_reduce[1];
}
$final_result[] = $actual_time_array[0]-$time_to_reduce[0];
return implode(":", array_reverse($final_result));
}
echo timeSubtactionFirstTime($actual_time ,$time_to_reduce);
注意:- 我在这里不考虑秒数。为此需要一点逻辑
将您的时间字符串转换为 DateInteval 字符串
$time1 = "02:40:00";
$time2 = "34:20:00";
list($h, $m, $s) = explode(':', $time1);
$time1 = new DateInterval("PT{$h}H{$m}M{$s}S");
list($h, $m, $s) = explode(':', $time2);
$time2 = new DateInterval("PT{$h}H{$m}M{$s}S");
$T1 = new DateTime('midnight');
$T2 = clone $T1;
$T1->add($time1)->sub($time2);
$diff = $T2->diff($T1);
echo ($diff->invert ? '-' : '') . ($diff->d*24 + $diff->h) . ':' .$diff->i;
我有自己的做法:
<?php
/**
* Calculate the number of hours and minutes between two times.
* @param string $start The start time
* @param string $end The end time
* @return string The difference in hours and minutes
*/
function timeDiff( $start, $end )
{
// Since the end time may be more than 23 hours ...
$seg = (int) substr( $end, 0, 2 );
// Add seconds to time2 ?
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
// Subtract hours from time 2 if necessary
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
// Compute the diff in seconds
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
// How many hours and minutes were there ?
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
// Pad the hours and minutes
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute;
}
// Simple test
$start = "02:40:00";
$end = "34:20:00";
echo timeDiff( $start, $end );
而如果你也想计算秒数,那就稍微修改一下:
<?php
/**
* Calculate the number of hours, minutes,
* and seconds between two times.
* @param string $start The start time
* @param string $end The end time
* @return string The difference in hours:minutes:seconds
*/
function timeDiff( $start, $end )
{
// Since the end time may be more than 23 hours ...
$seg = (int) substr( $end, 0, 2 );
// Add seconds to time2 ?
$add = $seg > 23
? ( $seg - 23 ) * 3600
: 0;
// Subtract hours from time 2 if necessary
if( $seg > 23 )
$end = substr_replace( $end, '23', 0, 2 );
// Compute the diff in seconds
$time1 = strtotime($start);
$time2 = strtotime($end) + $add;
$diff = $time2 - $time1;
// How many hours and minutes were there ?
$hour = floor( $diff / (60 * 60) );
$minute = $diff - $hour * (60 * 60);
$minute = floor( $minute / 60 );
$second = $diff - ( $hour * 60 * 60 ) - ( $minute * 60 );
// Pad the hours, minutes, and seconds
$hour = str_pad( $hour, 2, '0', STR_PAD_LEFT );
$minute = str_pad( $minute, 2, '0', STR_PAD_LEFT );
$second = str_pad( $second, 2, '0', STR_PAD_LEFT );
return $hour . ':' . $minute . ':' . $second;
}
// Simple test
$start = "02:40:00";
$end = "34:20:45";
echo timeDiff( $start, $end );