How to Calculate The Sum and Duration of Two Different Time Using PHP

There was a question that asked to me about how to sum and find out the duration of the two different time using PHP code regarding this my article. So, this following code will answer the question. You will be able to know how to calculate the sum and duration between two different time.

Here is the answer!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php
 
/**
 * @author Masino Sinaga, http://www.openscriptsolution.com
 * @copyright August 23, 2010
 */
 
$time1 = '12:12:20';
$time2 = '13:14:29';
echo 'Begin of Time: '.$time1;
echo '<br />End of Time: '.$time2;
echo '<br />The Sum of Time: '. sum_the_time($time1, $time2);  // this will give you a result: 19:12:25
echo '<br />Duration: '. duration_time($time1, $time2); // this will give you a result: 0 Day(s), 1 Hour(s), 2 Minute(s), 9 Second(s).
 
function sum_the_time($time1, $time2) {
  $times = array($time1, $time2);
  $seconds = 0;
  foreach ($times as $time)
  {
    list($hour,$minute,$second) = explode(':', $time);
    $seconds += $hour*3600;
    $seconds += $minute*60;
    $seconds += $second;
  }
  $hours = floor($seconds/3600);
  $seconds -= $hours*3600;
  $minutes  = floor($seconds/60);
  $seconds -= $minutes*60;
  // return "{$hours}:{$minutes}:{$seconds}";
  return sprintf('%02d:%02d:%02d', $hours, $minutes, $seconds); 
}
 
function duration_time($parambegindate, $paramenddate) {
  $begindate = strtotime($parambegindate);
  $enddate = strtotime($paramenddate);
  $diff = intval($enddate) - intval($begindate);                 
  $diffday = intval(floor($diff/86400));
  $modday = ($diff%86400);
  $diffhour = intval(floor($modday/3600));
  $diffminute = intval(floor(($modday%3600)/60));
  $diffsecond = ($modday%60);
  return round($diffday)." Day(s), ".round($diffhour)." Hour(s), ".round($diffminute,0)." Minute(s), ".round($diffsecond,0)." Second(s).";
}
 
?>

If you run that code, then it will output to your browser as following:

Begin of Time: 12:12:20
End of Time: 13:14:29
The Sum of Time: 25:26:49
Duration: 0 Day(s), 1 Hour(s), 2 Minute(s), 9 Second(s).
Share

1,869 viewsPrint This Post Print This Post

Comments

  1. Arlia Widy says:

    $diffday = intval(floor($diff/86400));

    how about month and year ?
    where did 86400 come from?

  2. Great post! 86400 came from the following: 24 hours x 60 minutes x 60 seconds = 86400 seconds in 1 day

  3. ika says:

    kalau mengitung selisih jam:menit: detik tanpa hari scriptnya gimana ya ?

    sudah coba yang di atas gak berhasil.,
    mohon bantuanya

    • Tinggal ganti baris berikut:

      return round($diffday)." Day(s), ".round($diffhour)." Hour(s), ".round($diffminute,0)." Minute(s), ".round($diffsecond,0)." Second(s).";

      menjadi seperti ini:

      return round($diffhour)." Hour(s), ".round($diffminute,0)." Minute(s), ".round($diffsecond,0)." Second(s).";
  4. Aneeq says:

    I have found a good solution on the site below. Works like charm for me.

    http://phphelp.co/2012/03/28/how-to-print-all-the-months-and-years-between-two-dates-in-php/

  5. Sahman says:

    Bang Masino,
    Bagaimana cara menghitung rata-rata waktu dari sebuah proses(yang berulang ulang tetapi waktunya berbeda) misalnya Rata-rata waktu kerja karyawan dalam sebulan.

    Field waktu start = wktin dan waktu out = wktout.
    Kalau different time saya sudah dapat dengan menggunakan timediff(wktout,wktin) as waktukerja. Nah, rata-rata waktukerja dalam sebulan saya pusing juga.

    Thanks Bang Masino, ada salam dari Rone Masintan Simanjuntak dari Medan.

    Sahman

    • Convert dulu hasil perhitungan selisih harian tadi menggunakan strtotime, lalu jumlahkan semuanya dalam sebulan, kemudian hasilnya bagi dengan jumlah hari dalam sebulan tadi, dan terakhir convert lagi menjadi format hh:mm:ss.

Speak Your Mind

*


*