0

I just want to output as a result of a script to be executed through CLI the time the execution of the script took.

for doing that, I set a var at the start of the script

$start_time = time();

and then at the end

date('H:i:s',time() - $start_time);

the problem is that even when the ellapsed time might be in the range of seconds or minutes, it always print that at least an hour has passed:

>>> echo date('H:i:s',1);
01:00:01
>>> echo date('H:i:s', 10);
01:00:10
>>> echo date('H:i:s',3599);
01:59:59
>>> echo date('H:i:s',3600);
02:00:00

shouldn't it display 00:XX:YY, when less than an hour has passed? is there something I'm missing, is there a bug?

thanks for your help!

msonsona
  • 1,306
  • 14
  • 20
  • 2
    You can't use `date()` this way: It works with timestamps. The value `1` is actually January 1st, 1970, 0:00:01 GMT, adjusted to your time zone. – Pekka Nov 04 '10 at 12:38

5 Answers5

2

Try this:

echo "Time left:". date('H:i:s', (strtotime('2000-01-01 00:00:00') + (time()-$start_time) ));
j0k
  • 22,600
  • 28
  • 79
  • 90
Alex
  • 41
  • 1
2

Don't use date(). When you have time() - $start_time the result is in seconds. Multiply this up if you want it in mintues etc. or use the following function to convert the seconds to Hours, Minutes and Seconds.

<?php /**
 *
 * @convert seconds to hours minutes and seconds
 *
 * @param int $seconds The number of seconds
 *
 * @return string
 *
 */
function secondsToWords($seconds) {
    /*** return value ***/
    $ret = "";

    /*** get the hours ***/
    $hours = intval(intval($seconds) / 3600);
    if($hours > 0)
    {
        $ret .= "$hours hours ";
    }
    /*** get the minutes ***/
    $minutes = bcmod((intval($seconds) / 60),60);
    if($hours > 0 || $minutes > 0)
    {
        $ret .= "$minutes minutes ";
    }

    /*** get the seconds ***/
    $seconds = bcmod(intval($seconds),60);
    $ret .= "$seconds seconds";

    return $ret;
} ?>

Example usage:

<?php
    /*** time since EPOCH ***/
    echo secondsToWords(time());
?>
taco
  • 1,367
  • 17
  • 32
drew
  • 1,312
  • 8
  • 20
2

You could try this function:

<?

$time = strtotime('2010-04-28 17:25:43');

echo 'event happened '.humanTiming($time).' ago';

function humanTiming ($time)
{

    $time = time() - $time; // to get the time since that moment

    $tokens = array (
        31536000 => 'year',
        2592000 => 'month',
        604800 => 'week',
        86400 => 'day',
        3600 => 'hour',
        60 => 'minute',
        1 => 'second'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
    }

}


?>

From here: PHP How to find the time elapsed since a date time?

This will give you a better means of getting the difference. Simply replace strtotime('2010-04-28 17:25:43'); with whatever your start date/time is (as a timestamp). One of the benefits is the function can be repurposed elsewhere.

Community
  • 1
  • 1
SW4
  • 69,876
  • 20
  • 132
  • 137
0

This function gives a "fully" formatted human readable time string..

    function humantime ($oldtime, $newtime = null, $returnarray = false)    {
        if(!$newtime) $newtime = time();
        $time = $newtime - $oldtime; // to get the time since that moment
        $tokens = array (
                31536000 => 'year',
                2592000 => 'month',
                604800 => 'week',
                86400 => 'day',
                3600 => 'hour',
                60 => 'minute',
                1 => 'second'
        );
        $htarray = array();
        foreach ($tokens as $unit => $text) {
                if ($time < $unit) continue;
                $numberOfUnits = floor($time / $unit);
                $htarray[$text] = $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
                $time = $time - ( $unit * $numberOfUnits );
        }
        if($returnarray) return $htarray;
        return implode(' ', $htarray);
    }
Arvind K.
  • 1,184
  • 2
  • 13
  • 27
0

Use gmdate() instead of date() to display the correct time difference and compensate for the server's time zone

bcosca
  • 17,371
  • 5
  • 40
  • 51
  • timezone shouldn't make a difference if your just taking a number of seconds away from another number of seconds. – drew Nov 04 '10 at 12:46
  • gmdate() seems to be returning proper values, but in the docs, it reads that the function is identical to date() and it also receives a timestamp as parameter – msonsona Nov 04 '10 at 12:58
  • @user495812: it makes a difference because the values specified in the above examples are absolute (based on GMT). – bcosca Nov 04 '10 at 13:08