Search code examples
phpdatetimeuptime

How to return the amount of years passed?


My friend and I are working on a fairly basic uptime script for an IRC Bot.

Here's our code:

function Uptime()
{
    global $uptimeStart;
    $currentTime = time();
    $uptime = $currentTime - $uptimeStart;
    $this->sendIRC("PRIVMSG {$this->ircChannel} :Uptime: ".date("z",$uptime)." Day(s) - ".date("H:i:s",$uptime));
}

$uptimeStart is set immediately when the script runs, as time();

for some reason when I execute this function, it starts at 364 days and 19 hours. I can't figure out why.


Solution

  • Your $uptime is not a timestamp as should be used in date(), but a difference in time. You have an amount of seconds there, not a timestamp (that corresponds with an actual date.

    just use something like this to cacluate (quick one, put some extra brain in for things like 1 day, 2 hours etc) ;)

     $minutes = $uptime / 60;
     $hours   = $minuts/60 ;
     $days    = $hours / 24
    

    etc