如何使用PHP从数组值中减去时间?

发布于 2025-01-29 10:25:10 字数 208 浏览 0 评论 0原文

我有这个数组。

$val = array(12:10, 4:16, 2:05);

我想从$ val数组中减去14:00 hrs,从每个元素都不会像这样的元素变为负面的方式:

$val = array(0, 2:26, 2:05)

I have this array.

$val = array(12:10, 4:16, 2:05);

I want to subtract 14:00 hrs from the $val array in the way that from every element that element do not become negative like this:

$val = array(0, 2:26, 2:05)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2025-02-05 10:25:10
<?php
$input = "14:00";
$val = array("12:10", "4:16", "2:05");

function timeSubtractionFirstTime($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));
}

$timeToReduceLeft = $input;
foreach ($val as &$value) {
    $diff = timeSubtractionFirstTime($value, $timeToReduceLeft);
    if (strpos($diff, chr(45)) !== false){ //if $value < $timeToReduceLeft
        $timeToReduceLeft = timeSubtractionFirstTime($timeToReduceLeft, $value);
        $value = "00:00";
    }
    else { //if $value >= $timeToReduceLeft
        $value = timeSubtractionFirstTime($value, $timeToReduceLeft);
        $timeToReduceLeft = "00:00";
    }
    
    
    if ($timeToReduceLeft == "00:00"){
        break;
    }

}

echo implode(",", $val);

我获得了从此 post 的功能您的问题的逻辑如下:

我们从输入值提取时间,直到输入值变为零或整个数组元素变为零(通过forloop)。

该解决方案还可以使用大于“ 24:00”的输入或数组元素值。

<?php
$input = "14:00";
$val = array("12:10", "4:16", "2:05");

function timeSubtractionFirstTime($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));
}

$timeToReduceLeft = $input;
foreach ($val as &$value) {
    $diff = timeSubtractionFirstTime($value, $timeToReduceLeft);
    if (strpos($diff, chr(45)) !== false){ //if $value < $timeToReduceLeft
        $timeToReduceLeft = timeSubtractionFirstTime($timeToReduceLeft, $value);
        $value = "00:00";
    }
    else { //if $value >= $timeToReduceLeft
        $value = timeSubtractionFirstTime($value, $timeToReduceLeft);
        $timeToReduceLeft = "00:00";
    }
    
    
    if ($timeToReduceLeft == "00:00"){
        break;
    }

}

echo implode(",", $val);

I get the function to extract time from this post and implement the logic for your problem as follows:

We substract the time from input value until the input value become zero or entire array elements become zero (by forloop).

This solution also works with input or array elements value greater than "24:00".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文