Jump to content

Php Time


Kingy

Recommended Posts

this should be pretty simple for a lot of you, but this is really annoying...

 

Im trying to make an online timer.. eg I have been online for so many days, hours, minutes, seconds etc etc

 

now im sure you would do it something like

 

$starttime = time();

 

and then when i call for current time

 

$currenttime = time() - $starttime;

 

the problem i am having is how do i turn this is 15 seconds, or 1 day 4 hours 7 minutes 39 seconds?

Link to comment
https://forums.phpfreaks.com/topic/86247-php-time/
Share on other sites

If you only need days hours mins seconds you can do something like this

<?php
      //convert timepassed to days hours and mins
      $starttime = 1200213497;
      $timepassed = time() - $starttime;
      $days     = ($timepassed-($timepassed%86400))/86400;
      $timepassed = $timepassed - $days*86400;
      $hours    = ($timepassed - ($timepassed%3600))/3600;
      $timepassed = $timepassed - $hours*3600;
      $mins     = ($timepassed - ($timepassed%60))/60;
      $seconds  = $timepassed%60;
echo $days.':'.$hours.':'.$mins.':'.$seconds;
?>

Link to comment
https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440553
Share on other sites

oh thank you so much, thats exactly what i needed..

 

one last question, what would be the easiest way to make it so if days and hours were = to zero it would only display minutes and seconds.

 

eg:

 

if ($days == 0) {

echo "$hours hours $mins minutes and $seconds seconds";

} else {

echo "$days days $hours hours $mins minutes and $seconds seconds";

}

 

would i have to go through and make an if statement for all of them..

 

if ($days == 0 && $hours == 0) {

}

 

??

Link to comment
https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440565
Share on other sites

Yeah if statements would work

<?php
      //convert timepassed to days hours and mins
      $starttime = 1200213487;
      $timepassed = time() - $starttime;
      $days     = ($timepassed-($timepassed%86400))/86400;
      $hours    = floor( ($timepassed%86400)/3600 );
      $mins     = floor( ($timepassed%3600)/60 );
      $seconds  = $timepassed%60;

      if($days > 0)
        echo $days.':';
      if($days > 0 && $hours > 0 )
        echo $hours.':';

echo $mins.':'.$seconds;
?>

Link to comment
https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440572
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.