OT: PHP elapsed time question...

Discussion in 'HTML' started by Noozer, Mar 24, 2006.

  1. Noozer

    Noozer Guest

    I've got a PHP question (it's my first PHP script)... Hopefully there's a
    quick answer.

    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 some stuff happening here

    $finish_time = mktime();

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

    ?>
    Noozer, Mar 24, 2006
    #1
    1. Advertising

  2. Noozer wrote:
    > I've got a PHP question (it's my first PHP script)... Hopefully there's a
    > quick answer.
    >
    > 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 some stuff happening here
    >
    > $finish_time = mktime();
    >
    > echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
    >
    > ?>


    Resolution, you are using the wrong function

    http://www.php.net/manual/en/function.microtime.php
    microtime()

    vs

    http://www.php.net/manual/en/function.mktime.php
    mktime()

    --
    Take care,

    Jonathan
    -------------------
    LITTLE WORKS STUDIO
    http://www.LittleWorksStudio.com
    Jonathan N. Little, Mar 24, 2006
    #2
    1. Advertising

  3. Noozer

    Roman Guest

    Noozer wrote:
    > I've got a PHP question (it's my first PHP script)... Hopefully there's a
    > quick answer.
    >
    > 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 some stuff happening here
    >
    > $finish_time = mktime();
    >
    > echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
    >
    > ?>


    In my opinion the problem is that date() function returns local time. So
    if you supply with integer number 42, that would translate into January
    1 1970 00:00:42 GMT, but your timezone is GMT+17 or GMT-7 hours, so
    that's where it comes from. Try gmdate().
    Roman, Mar 25, 2006
    #3
  4. Noozer

    Noozer Guest

    "Roman" <> wrote in message
    news:VZ8Vf.3928$...
    > Noozer wrote:
    >> I've got a PHP question (it's my first PHP script)... Hopefully there's a
    >> quick answer.
    >>
    >> 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 some stuff happening here
    >>
    >> $finish_time = mktime();
    >>
    >> echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
    >>
    >> ?>

    >
    > In my opinion the problem is that date() function returns local time. So
    > if you supply with integer number 42, that would translate into January
    > 1 1970 00:00:42 GMT, but your timezone is GMT+17 or GMT-7 hours, so
    > that's where it comes from. Try gmdate().


    My problem was thinking that a timestamp of 0 = "00/00/00 00:00:00" but
    instead means "1969/??/?? 17:??:??"

    What I was really trying to do was find the number of seconds between two
    timestamps and format it in HH:MM:SS. I didn't know that timestamps were
    actually measured in seconds either.

    "$finish_time-$start_time" gives me the number of seconds between two
    stamps, which is good enough. I would like to know if there is a simple way
    format it out to "HH:MM:SS" (i.e. 123 seconds would output "00:02:03")
    Noozer, Mar 25, 2006
    #4
  5. Noozer

    Roman Guest

    Noozer wrote:
    > "Roman" <> wrote in message
    > news:VZ8Vf.3928$...
    >> Noozer wrote:
    >>> I've got a PHP question (it's my first PHP script)... Hopefully there's a
    >>> quick answer.
    >>>
    >>> 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 some stuff happening here
    >>>
    >>> $finish_time = mktime();
    >>>
    >>> echo "Elapsed time was ".(date("G:i.s",$finish_time-$start_time)).".";
    >>>
    >>> ?>

    >> In my opinion the problem is that date() function returns local time. So
    >> if you supply with integer number 42, that would translate into January
    >> 1 1970 00:00:42 GMT, but your timezone is GMT+17 or GMT-7 hours, so
    >> that's where it comes from. Try gmdate().

    >
    > My problem was thinking that a timestamp of 0 = "00/00/00 00:00:00" but
    > instead means "1969/??/?? 17:??:??"
    >
    > What I was really trying to do was find the number of seconds between two
    > timestamps and format it in HH:MM:SS. I didn't know that timestamps were
    > actually measured in seconds either.
    >
    > "$finish_time-$start_time" gives me the number of seconds between two
    > stamps, which is good enough. I would like to know if there is a simple way
    > format it out to "HH:MM:SS" (i.e. 123 seconds would output "00:02:03")
    >
    >


    You got it right. The date() function as you used will print the time
    from timestamp, where 0 corresponds to 01/01/70 00:00:00, so if you omit
    the date part, you get string in your desired format. Only you have to
    use GMT functions, otherwise the time-zone offset will be added.

    I am pretty sure the date function is internally complicated and slow
    for this purpose and there is probably a better way to do it. I am just
    learning PHP and its libraries and I am not sure what is the resemblance
    of PHP's printf() to the one in standard C library, but here is the C way:

    printf("%02d:%02d:%02d", (span/3600)%24, (span/60)%60, span%60);

    where variable span is the time difference.

    Had you posted this question in in comp.lang.php, you wuld get more and
    better advices.

    Roman
    Roman, Mar 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. spider12

    elapsed time of a program

    spider12, Jul 20, 2004, in forum: Java
    Replies:
    5
    Views:
    983
    Hemal Pandya
    Jul 21, 2004
  2. Thomas Baier

    getting elapsed time in milliseconds

    Thomas Baier, Sep 20, 2003, in forum: C++
    Replies:
    6
    Views:
    24,950
    J. Campbell
    Sep 22, 2003
  3. Peter Hansen
    Replies:
    13
    Views:
    663
    Peter Hansen
    Jul 7, 2005
  4. Rajive Narain
    Replies:
    0
    Views:
    1,556
    Rajive Narain
    Sep 18, 2009
  5. rutherf
    Replies:
    2
    Views:
    409
    rutherf
    Oct 28, 2006
Loading...

Share This Page