killah Posted April 1, 2009 Share Posted April 1, 2009 Well, i was coding this little feature which will display 1 Day, 2 hours, 3 minutes and 4 seconds ago. If that is the unix time. How ever, it displays way off. I am currently trying to display.. $fn->time(time()); $fn being the global function to my function's class. This is my php code. <?php //Added for the colours function time($urtime) { $seconds = floor($urtime); $days = intval($seconds / 1440); $seconds -= ($days * 1400); $hours = intval($seconds / 360); $seconds -= ($hours * 360); $minutes = intval($seconds / 60); $seconds -= ($minutes * 60); $result = array(); $result[] = ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE; $result[] = ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE; $result[] = ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE; $result[] = ($seconds) ? sprintf('and %u second%s', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE; return ($result == 0) ? 'Never' : implode(' ', $result).' ago'; } If anyone can help figure out why it's doing this. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/ Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 Found this little snippet function search($session_time) { //$session_time ="1151348975"; echo"You Entered: $session_time <BR>"; echo"The Current Time is = ".time()."<BR>"; $time_difference = time() - $session_time ; echo"time_difference = ".$time_difference."<HR>"; $seconds = $time_difference ; $minutes = round($time_difference / 60 ); $hours = round($time_difference / 3600 ); $days = round($time_difference / 86400 ); $weeks = round($time_difference / 604800 ); $months = round($time_difference / 2419200 ); $years = round($time_difference / 29030400 ); echo"seconds: $seconds<br>"; echo"minutes: $minutes<br>"; echo"hours: $hours<br>"; echo"days: $days<br>"; echo"weeks: $weeks<br>"; echo"months: $months<br>"; echo"years: $years<br>"; } From: http://www.wallpaperama.com/forums/learn-php-time-function-to-display-time-hours-days-weeks-months-years-t1033.html Hopefully that helps ya out. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798760 Share on other sites More sharing options...
killah Posted April 1, 2009 Author Share Posted April 1, 2009 I'm going to work off that script and figure something out. So far it's working out good. How ever, the second's go over 60. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798867 Share on other sites More sharing options...
killah Posted April 1, 2009 Author Share Posted April 1, 2009 Hmmm, no seem to get no where. I am currently using: function time($urtime) { $seconds = round(time() - $urtime); $minutes = intval((time() - $urtime) / $seconds); $hours = intval((time() - $urtime) / 3600); $days = intval((time() - $urtime) / 86400); $weeks = intval((time() - $urtime) / 604800); $months = intval((time() - $urtime) / 2419200); $years = intval((time() - $urtime) / 29030400); $result = array(); $result[] = ($years) ? sprintf('%u year%s,', number_format($years), ($years > 1) ? 's' : '') : FALSE; $result[] = ($months) ? sprintf('%u month%s,', number_format($months), ($months > 1) ? 's' : '') : FALSE; $result[] = ($weeks) ? sprintf('%u week%s,', number_format($weeks), ($weeks > 1) ? 's' : '') : FALSE; $result[] = ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE; $result[] = ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE; $result[] = ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE; $result[] = ($seconds) ? sprintf('%s %u second%s',($minutes) ? 'and' : '', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE; return (count($result) == 0) ? 'Never' : implode(' ', $result).' ago'; } Then displaying using: echo $fn->time(1238539180); It seem's to be displaying: 19 hours, 1 minute and 71793 seconds ago. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798886 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 I think this would work.... $seconds = (((time() - $urtime)/60)%60); You want the remainder of the difference divided by 60. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798899 Share on other sites More sharing options...
killah Posted April 1, 2009 Author Share Posted April 1, 2009 Now my display is: 20 hours, 1234 minutes and 34 seconds ago and the 34 second's only go up in minute's. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798923 Share on other sites More sharing options...
premiso Posted April 1, 2009 Share Posted April 1, 2009 $minutes = intval(round((time() - $urtime)/60)); $seconds = round($minutes%60); I would order it that way, since we use the minutes to calculate the seconds. See if that does it right. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798925 Share on other sites More sharing options...
killah Posted April 1, 2009 Author Share Posted April 1, 2009 This actualy did it for me. function time($urtime) { $seconds = (((time() - $urtime) / 1) % 60); $minutes = (intval((time() - $urtime) / 60) % 60); $hours = intval((time() - $urtime) / 3600); $days = intval((time() - $urtime) / 86400); $weeks = intval((time() - $urtime) / 604800); $months = intval((time() - $urtime) / 2419200); $years = intval((time() - $urtime) / 29030400); $result = array(); $result[] = ($years) ? sprintf('%u year%s,', number_format($years), ($years > 1) ? 's' : '') : FALSE; $result[] = ($months) ? sprintf('%u month%s,', number_format($months), ($months > 1) ? 's' : '') : FALSE; $result[] = ($weeks) ? sprintf('%u week%s,', number_format($weeks), ($weeks > 1) ? 's' : '') : FALSE; $result[] = ($days) ? sprintf('%u day%s,', number_format($days), ($days > 1) ? 's' : '') : FALSE; $result[] = ($hours) ? sprintf('%u hour%s,', number_format($hours), ($hours > 1) ? 's' : '') : FALSE; $result[] = ($minutes) ? sprintf('%u minute%s', $minutes, ($minutes > 1) ? 's' : FALSE) : FALSE; $result[] = ($seconds) ? sprintf('%s %u second%s',($minutes) ? 'and' : '', $seconds, ($seconds > 1) ? 's' : FALSE) : FALSE; return (count($result) == 0) ? 'Never' : implode(' ', $result).' ago'; } Thank's for the help you provided. Quote Link to comment https://forums.phpfreaks.com/topic/152093-solved-php-time-off/#findComment-798927 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.