For Programmers: Free Programming Magazines  


Home > Archive > PHP Language > March 2006 > Elapsed time between two timestamps?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Elapsed time between two timestamps?
Noozer

2006-03-24, 7:57 am

Why does the following show a 17 hour difference when the "Some Stuff" code
actually takes only 42 seconds?

For output I get:
Elapsed time was 17:00.42.


<?php

$start_time = mktime();

//42 seconds of stuff happens here

$finish_time = mktime();

echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";

?>


Christian Hansel

2006-03-24, 7:57 am

Noozer wrote:

> Why does the following show a 17 hour difference when the "Some Stuff"
> code actually takes only 42 seconds?
>
> For output I get:
> Elapsed time was 17:00.42.
>
>
> <?php
>
> $start_time = mktime();
>
> //42 seconds of stuff happens here
>
> $finish_time = mktime();
>
> echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
>
> ?>



The output is correct. date requires a timestamp as second parameter.

try: date("YmdHis",$finish_time-$start_time)

and you will understand.

What you want is

echo "Elapsed time was ".($finish_time-$start_time)."seconds.";
Noozer

2006-03-24, 7:57 am

>> echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
> The output is correct. date requires a timestamp as second parameter.
>
> try: date("YmdHis",$finish_time-$start_time)


AHA! a timestamp of 0 means around 5PM on a date in 1969...

> and you will understand.
>
> What you want is
>
> echo "Elapsed time was ".($finish_time-$start_time)."seconds.";


I didn't realize that the timestamp was measured in seconds.

Now, is there a simple format command that would change, say 66 seconds to
"1:06" ?


Thanks muchly!!!


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com