3

I want sum of 3 different times.

Ex:

h1 = "04:23";
h2 = "04:16";
h3 = "00:00";

So answer should be $hours = "08:39"

I have searched for that and got many solution but not getting right answer.

Here is my code:

$hours = strtotime($h1) + strtotime($h2) + strtotime($h3);
$total_hours = date("H:i", intval($hours));
echo $total_hours;

"00:00" is the answer which am getting for above code. I don't want to use any user define function which can add times. Can anyone please help me?

15
  • You want time or the total hours & minutes? Commented Dec 23, 2016 at 11:08
  • Total hours and minutes. Commented Dec 23, 2016 at 11:09
  • I am not sure if there is a built in method. But what is wrong with calculating the total time? Commented Dec 23, 2016 at 11:11
  • I have read somewhere that calculating total time is not a good way and there is some way to get summation of times but I don't know that way. Commented Dec 23, 2016 at 11:12
  • 2
    Possible duplicate of How to sum N number of time (HH:MM Format) ?? Commented Dec 23, 2016 at 11:15

3 Answers 3

3

You want total time and hours so no need to work on strtotime or date() method. There isn't any inbuilt method for this solution. I always use this solution.

Working Demo for your solution : https://eval.in/98249

function sum_time() {
  $i = 0;
  foreach (func_get_args() as $time) {
    sscanf($time, '%d:%d', $hour, $min);
    $i += $hour * 60 + $min;
  }
  if ($h = floor($i / 60)) {
    $i %= 60;
  }
  return sprintf('%02d:%02d', $h, $i);
}

echo sum_time('04:23', '04:16', '00:00');
Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to use any user define function which can add times.
@SougataBose. yeah i read it on question but there isn't any way i have spent a lot of time for this logic but it's not possible so many possibilities are there like when day change etc. so above solution is the solution as per my knowledge
0

If you don't want to use user-defined logic, your only option is to use built-in function which is strtotime as per my opinion. But you need to provide right format to strtotime function which is like 00:04:16. I've provided my logic to give you the solution.

$h1 = "04:23";
$h2 = "04:16";
$h3 = "00:00";

$h_arr = array($h1, $h2, $h3);

function sum_time($h_arr) {
    $hours = 0;
    $minutes = 0;
    foreach($h_arr as $value) {
        $value_explode = explode(':', $value);
        $hours += $value_explode[0];
        $minutes += $value_explode[1];
        if($minutes > 60) {
            $hours += 1;
            $minutes = $minutes % 60;
        }
    }

    return sprintf('%02d:%02d', $hours, $minutes);
}

echo sum_time($h_arr);

The logic provided by Bhavin is also good. If you're comfortable with it, you could go for that. It's up to you.

Demo

With your test cases - https://eval.in/703528

Other test cases - https://eval.in/703529

Comments

0

Bhavin and Perumal93 answer working fine. But i am thinking different way.I am trying to do this as simple as possible. Code look like below:

<?php
/**
 * @param array $times
 * @return string
 */
function sumOfDiffrentTime($times = array())
{
    $minutes = '';
    // loop through all the times array
    foreach ($times as $time) {
        list($hour, $minute) = explode(':', $time);
        $minutes += $hour * 60;
        $minutes += $minute;
    }
    $hours = floor($minutes / 60);
    $minutes -= $hours * 60;

    // returns the  formatted time
    return sprintf('%02d:%02d', $hours, $minutes);
}

$times = array();

$times[] = "04:23";
$times[] = "04:16";
$times[] = "00:00";

// pass your $times array to the function
echo sumOfDiffrentTime($times);

LIVE DEMO

Hope it will helps you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.