seany123 Posted July 27, 2009 Share Posted July 27, 2009 okay what i have is a database table with a timestamp in it.. so when i echo this echo "<td>" . $spy['time'] . "</td>\n"; it displays the time and date.... well what i wanna do is display the time since the timestamp. so if it was 10 minutes ago it will say 10m or whatever.. so i tried using this... echo "<td>" . timeSince($spy['time']) . "</td>\n"; but its echoing this: 43y 217d 16h 31m 37s thats obviously wrong because i wasnt even alive then lol. does anyone know why this is happening? and how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/ Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 Either the input of the function is incorrect, or the algorithm defined by the function is incorrect. It's difficult to tell without having the source of timeSince(). Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884048 Share on other sites More sharing options...
seany123 Posted July 27, 2009 Author Share Posted July 27, 2009 i didnt write this... but here it is. function timeSince($urtime) { $seconds = (((time() - $urtime) / 1) % 60); $minutes = (intval((time() - $urtime) / 60) % 60); $hours = intval((time() - $urtime) / 3600) % 24; $days = intval((time() - $urtime) / 86400) % 365; $years = intval((time() - $urtime) / 29030400); $result = array(); $result['years'] = ($years) ? sprintf('%uy%s', number_format($years), ($years > 1) ? '' : '') : FALSE; $result['days'] = ($days) ? sprintf('%ud%s', number_format($days), ($days > 1) ? '' : '') : FALSE; $result['hours'] = ($hours) ? sprintf('%uh%s', number_format($hours), ($hours > 1) ? '' : '') : FALSE; $result['minutes'] = ($minutes) ? sprintf('%um%s', $minutes, ($minutes > 1) ? '' : FALSE) : FALSE; $result['seconds'] = ($seconds) ? sprintf('%s%us%s',($minutes) ? '' : '', $seconds, ($seconds > 1) ? '' : FALSE) : FALSE; if ($seconds < 1) return "NOW!"; else return (count($result) == 0) ? 'Never' : implode(' ', $result).''; } Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884051 Share on other sites More sharing options...
waynew Posted July 27, 2009 Share Posted July 27, 2009 I made this class ages ago but I still use it as it works perfectly: <?php class TimeSince{ static function since($time){ $now = mktime(); $seconds_since = $now - $time; if($seconds_since < 60){ return $seconds_since.' seconds ago'; } else if($seconds_since > 59 && $seconds_since < 120){ return '1 minute ago'; } else if($seconds_since > 119 && $seconds_since < 3600){ $minutes = round($seconds_since / 60); return $minutes.' minutes ago'; } else if($seconds_since > 3599 && $seconds_since < 86400){ $hours = round($seconds_since / 3600); if($hours == 1){ return "1 hour ago"; } else{ return $hours.' hours ago'; } } else if($seconds_since > 86400 && $seconds_since < 604800){ $days = round($seconds_since/86400); if($days == 1){ return "1 day ago"; } else{ return $days.' days ago'; } } else if($seconds_since > 604799){ $weeks = round($seconds_since/604800); if($weeks == 1){ return "1 week ago"; } else{ return $weeks.' weeks ago'; } } } } ?> To use it just do: require_once("time.class.php"); //or whatever filename you give the above code $timesince = strtotime($spy['time']); echo TimeSince::since($timesince); Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884052 Share on other sites More sharing options...
Daniel0 Posted July 27, 2009 Share Posted July 27, 2009 You said: so when i echo this echo "<td>" . $spy['time'] . "</td>\n"; it displays the time and date.... That would indicate a formatted string, but your function seems to take a UNIX timestamp as argument. That could possibly explain the unexpected behavior. Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884065 Share on other sites More sharing options...
seany123 Posted July 27, 2009 Author Share Posted July 27, 2009 I made this class ages ago but I still use it as it works perfectly: <?php class TimeSince{ static function since($time){ $now = mktime(); $seconds_since = $now - $time; if($seconds_since < 60){ return $seconds_since.' seconds ago'; } else if($seconds_since > 59 && $seconds_since < 120){ return '1 minute ago'; } else if($seconds_since > 119 && $seconds_since < 3600){ $minutes = round($seconds_since / 60); return $minutes.' minutes ago'; } else if($seconds_since > 3599 && $seconds_since < 86400){ $hours = round($seconds_since / 3600); if($hours == 1){ return "1 hour ago"; } else{ return $hours.' hours ago'; } } else if($seconds_since > 86400 && $seconds_since < 604800){ $days = round($seconds_since/86400); if($days == 1){ return "1 day ago"; } else{ return $days.' days ago'; } } else if($seconds_since > 604799){ $weeks = round($seconds_since/604800); if($weeks == 1){ return "1 week ago"; } else{ return $weeks.' weeks ago'; } } } } ?> To use it just do: require_once("time.class.php"); //or whatever filename you give the above code $timesince = strtotime($spy['time']); echo TimeSince::since($timesince); i just tried that but now its echos "14 hours ago" Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884066 Share on other sites More sharing options...
waynew Posted July 27, 2009 Share Posted July 27, 2009 I made this class ages ago but I still use it as it works perfectly: <?php class TimeSince{ static function since($time){ $now = mktime(); $seconds_since = $now - $time; if($seconds_since < 60){ return $seconds_since.' seconds ago'; } else if($seconds_since > 59 && $seconds_since < 120){ return '1 minute ago'; } else if($seconds_since > 119 && $seconds_since < 3600){ $minutes = round($seconds_since / 60); return $minutes.' minutes ago'; } else if($seconds_since > 3599 && $seconds_since < 86400){ $hours = round($seconds_since / 3600); if($hours == 1){ return "1 hour ago"; } else{ return $hours.' hours ago'; } } else if($seconds_since > 86400 && $seconds_since < 604800){ $days = round($seconds_since/86400); if($days == 1){ return "1 day ago"; } else{ return $days.' days ago'; } } else if($seconds_since > 604799){ $weeks = round($seconds_since/604800); if($weeks == 1){ return "1 week ago"; } else{ return $weeks.' weeks ago'; } } } } ?> To use it just do: require_once("time.class.php"); //or whatever filename you give the above code $timesince = strtotime($spy['time']); echo TimeSince::since($timesince); i just tried that but now its echos "14 hours ago" Yes? It starts with seconds, then minutes, then hours, then days and finally weeks. Seeing "19028 minutes ago" is pretty messy. Quote Link to comment https://forums.phpfreaks.com/topic/167638-solved-timestamp-timesince-help-please/#findComment-884086 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.