MrXander Posted July 22, 2008 Share Posted July 22, 2008 Hi Guys, Trying to display, from a UNIX timestamp, how long it has been since that timestamp, but only showing years & days. Could someone help? Thanks! Quote Link to comment Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 unix timestamp is time in seconds. $then = 1216757988; // previous timestamp (example number) $now = time(); // current timestamp $timepassed = $now - $then; // elapsed time in seconds 60 seconds in a minute 60 minutes in an hour 24 hours in a day 365 days a year $timepassed / 60 gives you how many minutes passed $timepassed / 60 / 60 gives you how many hours passed $timepassed / 60 / 60 / 24 gives you how many days passed $timepassed / 60 / 60 / 24 / 365 gives you how many years passed I mean, other than accounting for leap year... what exactly do you need help on? This is simple math... Quote Link to comment Share on other sites More sharing options...
MrXander Posted July 22, 2008 Author Share Posted July 22, 2008 Sorry, maybe I didn't express myself very clearly. For example, if the unix timestamp read 1st July 2000, I want it to show the following: 8 years, 22 days How would I go about that? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted July 22, 2008 Share Posted July 22, 2008 Umm, Re-read CV's post again. Its all explained in there. Ignore the timepassed just use time() function Quote Link to comment Share on other sites More sharing options...
MrXander Posted July 22, 2008 Author Share Posted July 22, 2008 Umm, Re-read CV's post again. Its all explained in there. Ignore the timepassed just use time() function That post wouldn't do it though. Take his example for years... it would show as 8.6 (or there abouts). What I want it to do is stop at the closest year, then count the left overs as days. So, if the timestamp was for 1st July 2000 and todays date is 21st July 2008, I need it to say 8 years and 20 days, instead of ~8.6 or 2848 days. See what I mean? Quote Link to comment Share on other sites More sharing options...
.josh Posted July 22, 2008 Share Posted July 22, 2008 The unix timestamp wouldn't read 1st July 2000. It's an integer of the number of seconds since the unix epoch (January 1 1970 00:00:00 GMT). <?php $then = strtotime("1st July 2000"); $now = time(); $timepassed = $now - $then; $totaldays = $timepassed / 60 / 60 / 24; $years = (int) ($totaldays / 365); $days = $totaldays % 365; echo "$years years, $days days"; ?> Quote Link to comment Share on other sites More sharing options...
MrXander Posted July 22, 2008 Author Share Posted July 22, 2008 The unix timestamp wouldn't read 1st July 2000. It's an integer of the number of seconds since the unix epoch (January 1 1970 00:00:00 GMT). I know, I was just using that as an example... <?php $then = strtotime("1st July 2000"); $now = time(); $timepassed = $now - $then; $totaldays = $timepassed / 60 / 60 / 24; $years = (int) ($totaldays / 365); $days = $totaldays % 365; echo "$years years, $days days"; ?> That's spot on, thank you!! Quote Link to comment 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.