How can i calculate total hours in php?

I have a small issue while calculating sum of total hours

My working hours is like:

Day1 - 12:23 Hours
Day2 - 11:43 Hours
Day3 - 10:18 Hours

How can I get it by php? Thanks in advance

Noich

13.7k15 gold badges61 silver badges90 bronze badges

asked Mar 13, 2013 at 12:02

0

Just add them like this:

$day1hours = "12:23";
$day2hours = "11:43";
$day1 = explode(":", $day1hours);
$day2 = explode(":", $day2hours);

$totalmins = 0;
$totalmins += $day1[0] * 60;
$totalmins += $day1[1];
$totalmins += $day2[0] * 60;
$totalmins += $day2[1];

$hours = $totalmins / 60;
$minutes = $totalmins % 60;

$totalhours = "$hours.$minutes";

It is quite elaborate like this, but it just to show the idea: calculate back to minutes, add minutes together, then calculate back to hours.

Note that PHP has quite extensive support for dates and times. Consider using that: http://www.php.net/manual/en/book.datetime.php

How can i calculate total hours in php?

admdrew

3,7404 gold badges26 silver badges39 bronze badges

answered Mar 13, 2013 at 12:06

How can i calculate total hours in php?

Bart FriederichsBart Friederichs

32.3k15 gold badges96 silver badges187 bronze badges

This technique will give exact total hours and minutes. The above answer gives me 1.25.15 when i add 00:30 and 00:45

<?php
$hourMin = array("00:30","00:45"); //an Multiple Hour:Minutes array
$hours = 0;
$mins  = 0;
  foreach($hourMin as $val)
  {
      $explodeHoursMins = explode(':',$val);
      $hours += $explodeHoursMins[0];
      $mins  += $explodeHoursMins[1];
  }

  $minToHours =  date('H:i', mktime(0,$mins)); //Calculate Hours From Minutes
  $explodeMinToHours = explode(':',$minToHours);
  $hours += $explodeMinToHours[0];
  $finalMinutes = $explodeMinToHours[1];
  echo $hours." : ".$finalMinutes;
?>

answered Feb 2, 2018 at 11:46

How can i calculate total hours in php?

Vishnu BhadoriyaVishnu Bhadoriya

1,6161 gold badge21 silver badges28 bronze badges

If you use this function to calculate time calculation then it will work

function sumTime($initialHour, $finalHour) {
    $h = date('H', strtotime($finalHour));
    $m = date('i', strtotime($finalHour));
    $s = date('s', strtotime($finalHour));
    $tmp = $h." hour ".$m." min ".$s." second";
    $sumHour = $initialHour." + ".$tmp;
    $newTime = date('H:i:s', strtotime($sumHour));
    return $newTime;
 };

For more explanation refer this link: Time Calculation in php

answered Mar 13, 2013 at 12:16

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given an array containing the time in hr:min:sec format. The task is to calculate the total time. If the total time is greater then 24 hours then the total time will not start with 0. It will display the total time. There are two ways to calculate the total time from the array. 
     

    1. Using strtotime() function
    2. Using explode() function

    Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format.
    Syntax 
     

    strtotime( string )

    Example 1: This example reads the values from the array and converts it into the time format.
     

    php

    <?php

    $time = [

        '00:04:35', '00:02:06', '01:09:12',

        '09:19:04', '00:17:49', '02:13:59',

        '10:10:54'

    ];

    $sum = strtotime('00:00:00');

    $totaltime = 0;

    foreach( $time as $element ) {

        $timeinsec = strtotime($element) - $sum;

        $totaltime = $totaltime + $timeinsec;

    }

    $h = intval($totaltime / 3600);

    $totaltime = $totaltime - ($h * 3600);

    $m = intval($totaltime / 60);

    $s = $totaltime - ($m * 60);

    echo ("$h:$m:$s");

    ?>

    Using explode() function: The explode() function is used to break a string into an array.
    Syntax 
     

    array explode( separator, string, limit )

    Example 2: This example reads the values from an array and converts it into the time format.
     

    php

    <?php

    $arr = [

        '00:04:35', '00:02:06', '01:09:12',

        '09:19:04', '00:17:49', '02:03:59',

        '10:10:54'

    ];

    $total = 0;

    foreach( $arr as $element):

        $temp = explode(":", $element);

        $total+= (int) $temp[0] * 3600;

        $total+= (int) $temp[1] * 60;

        $total+= (int) $temp[2];

    endforeach;

    $formatted = sprintf('%02d:%02d:%02d',

                    ($total / 3600),

                    ($total / 60 % 60),

                    $total % 60);

    echo $formatted;

    ?>


    How do I calculate total hours in PHP?

    There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format.

    How can I calculate hours between two dates in PHP?

    $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1); Dividing by 3600 because there are 3600 seconds in one hour and using round() to avoid having a lot of decimal places.

    How can I add hours and minutes in PHP?

    php function CalculateTime($times) { $i = 0; foreach ($times 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); } $date[] = '02:32'; $date[] = '01:29'; echo CalculateTime($date); ?>

    How is start time and end time calculated in PHP?

    “calculate total time from start and end datetime in php” Code Answer.
    date1 = new DateTime('2006-04-12T12:30:00');.
    $date2 = new DateTime('2006-04-14T11:30:00');.
    $diff = $date2->diff($date1);.
    $hours = $diff->h;.
    $hours = $hours + ($diff->days*24);.