Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.