Calculate elapsed time in php?

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

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.

1 $first = new DateTime($_SESSION'h'. ':'. $_SESSION'm'.':'.

$_SESSION's'); works for making a DateTime object out of the given Session data. – Duroth Oct 21 at 13:56 unfortunately I'm using php 5.2.6 – wilest Oct 21 at 14:01.

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.

Also make sure you check out PHP date() function. Link – donutdan4114 Oct 21 at 13:48.

This will give you the number of seconds between start and end. To display it clock-style afterwards, you'd do something like this: 0) { $h++; $s -= 3600; } while ($s / 60 > 0) { $m++; $s -= 60; } return lz($h). ":".

Lz($m). ":". Lz($s); } function lz($num) { if ($num If you don't want to display the numbers after the decimal, just add round($s); to the beginning of the secondsToTime() function.

You did not read the question properly. He doesn't want to measure the runtime of his script, but rather an arbitrary timespan. – Till Helge Helwig Oct 21 at 13:45 I aknowledge it might be misleading, but imho he's not asking for benchmarking, but for calculating a simple time difference.

Think at, let's say, a travel duration, or a race, or something like that – Damien Pirsy Oct 21 at 13:45 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. – wilest Oct 21 at 13:48 1 That would change my answer a little bit, though the other recommendations would back up the idea of storing the times you'd like to compare as either DateTime objects or as UNIX timestamps.Do all your work in the same unit (in this case seconds) and then convert back to a more display-friendly time.

– Crontab Oct 21 at 13:56.

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.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions