Kingy Posted January 16, 2008 Share Posted January 16, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/ Share on other sites More sharing options...
teng84 Posted January 16, 2008 Share Posted January 16, 2008 if you're getting the stored time in your DB all you need to use datediff() timediff() which are sql functions Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440541 Share on other sites More sharing options...
Kingy Posted January 16, 2008 Author Share Posted January 16, 2008 nah i'm not planning on using sql.. it will be more along the links of <?php $starttime = time(); while(blahblah) { if the `time command is requested it will then get the time now and compare it to the start time } ?> Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440545 Share on other sites More sharing options...
revraz Posted January 16, 2008 Share Posted January 16, 2008 http://www.ilovejackdaniels.com/php/php-datediff-function/ Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440547 Share on other sites More sharing options...
Kingy Posted January 16, 2008 Author Share Posted January 16, 2008 thats a very large and confusing function to me, but thanks anyway i guess i'll try and look through it removing stuff i don't need :s Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440552 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440553 Share on other sites More sharing options...
Kingy Posted January 16, 2008 Author Share Posted January 16, 2008 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) { } ?? Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440565 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440572 Share on other sites More sharing options...
Daukan Posted January 16, 2008 Share Posted January 16, 2008 Oops, that code logic won't actually work as is, and my edit time expired... Quote Link to comment https://forums.phpfreaks.com/topic/86247-php-time/#findComment-440578 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.