How to calculate time in php

Hi All I'm trying to calculate elapsed time in php. The problem is not in php, it's with my mathematical skills. For instance: Time In: 11:35:20 (hh:mm:ss), now say the current time is: 12:00:45 (hh:mm:ss) then the time difference in my formula gives the output: 1:-34:25. It should actually be: 25:25

$d1=getdate();
$hournew=$d1['hours'];
$minnew=$d1['minutes'];
$secnew=$d1['seconds'];

$hourin = $_SESSION['h'];
$secin = $_SESSION['s'];
$minin = $_SESSION['m'];

$h2=$hournew-$hourin;
$s1=$secnew-$secin;
$m1=$minnew-$minin;

if($s1<0) {
    $s1+=60; }
if($s1>=(60-$secin)) {
    $m1--;  }
if($m1<0) {
    $m1++; }
echo $h2 . ":" . $m1 . ":" . $s1;

Any help please?

EDIT

Sorry I probably had to add that the page refreshes every second to display the new elapsed time so I have to use my method above. My apologies for not explaining correctly.

asked Oct 21, 2011 at 13:42

How to calculate time in php

WilestWilest

1,7347 gold badges33 silver badges62 bronze badges

3

This will give you the number of seconds between start and end.


To display it clock-style afterwards, you'd do something like this:


If you don't want to display the numbers after the decimal, just add round($s); to the beginning of the secondsToTime() function.

answered Oct 21, 2011 at 13:44

WWWWWW

9,4541 gold badge27 silver badges32 bronze badges

6

Using PHP >= 5.3 you could use DateTime and its method DateTime::diff(), which returns a DateInterval object:

$first  = new DateTime( '11:35:20' );
$second = new DateTime( '12:00:45' );

$diff = $first->diff( $second );

echo $diff->format( '%H:%I:%S' ); // -> 00:25:25

answered Oct 21, 2011 at 13:53

Decent DabblerDecent Dabbler

22.1k8 gold badges73 silver badges104 bronze badges

3

Keep track of your time using the 'time()' function. You can later convert 'time()' to other formats.

$_SESSION['start_time'] = time();

$end_time = time();

$end_time - $_SESSION['start_time'] = 65 seconds (divide by 60 to get minutes)

And then you can compare that to another value later on.

Use microtime if you need millisecond detail.

answered Oct 21, 2011 at 13:47

donutdan4114donutdan4114

1,2621 gold badge8 silver badges16 bronze badges

1

You can implement the solutions shown, but I'm fond of using the phptimer class (or others, this wheel has been invented a few times). The advantage is that you can usually define the timer to be active or not, thereby permitting you to leave the timer calls in your code for later reference without re-keying all the time points.

answered Oct 21, 2011 at 14:18

Duane GranDuane Gran

4904 silver badges9 bronze badges

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

    $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

    $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 can 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. Example 1: This example reads the values from the array and converts it into the time format.

    How does php calculate code execution time?

    Clock time can get using microtime() function. First use it before starts the script and then at the end of the script. Then using formula (End_time – Start_time). The mirotime() function returns time in seconds.

    How does php calculate start and end time?

    calculate total time from start and end datetime in php.
    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);.

    How can I calculate hours between two dates in php?

    You can convert them to timestamps and go from there: $hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);