Welcome to WebmasterWorld Guest from 188.8.131.52 , register , login , search , subscribe , help , library , PubCon , announcements , recent posts , open posts Subscribe to WebmasterWorld
PHP Time to "Number of Days Ago" cookiemonster msg:4479797 9:47 pm on Jul 28, 2012 (gmt 0) Hey WebmasterWorld folks! Haven't posted here in a long time! I have a question that I'm guessing will be pretty easy for most of you to answer, but I've been stuck on it for a while now (even after lots of Google searching). I have a string representing the date of an item which looks like this:
(2010 is the year, 12 is the month, 31 is the day, the rest is irrelevant). Now I need to convert this string into a simple number of days ago, relative to today's date. Just a simple integer - I don't want to switch to weeks or hours or years or anything. Just days. So my question is, what is the most efficient way of accomplishing this task? It seems to me like it would be fairly simple but I can't seem to figure it out. Thank you in advance! PS. I'm not "asking you to do my work for me" - just a suggestion or a link to get me on the right track would be enough! :D Michael
cookiemonster msg:4479811 10:48 pm on Jul 28, 2012 (gmt 0)
UPDATE: No replies but I got it figured out thanks to a post at StackOverflow: [ ...] stackoverflow.com It was as simple as
$now = time();
$date = strtotime('20101231120103'); $datediff = $now - $date; echo floor($datediff/(60*60*24)); Just as I suspected! Thanks anyways! g1smd msg:4479815 11:15 pm on Jul 28, 2012 (gmt 0)
And how do you take time zones into account? Do you store the data all converted to UTC? Something that happens midday "today" in the US is "zero days ago", but New Zealand already has tomorrows date. You need the data stored in a consistent time zone, use UTC. If you don't take time zones into account and just one second after the event has happened someone in New Zealand checked the age, it would incorrectly say "one day ago".
MaxPowers msg:4480683 8:18 pm on Jul 31, 2012 (gmt 0)
Just because... A Unix Timestamp given by time(); or date('U'); would be shorter than your timestamp, have an accuracy to the nearest second, and could be turned into any sort of date-related data in a flash using mktime() or date() and your adjustments would be simple time-based math: 3600 seconds in an hour 86400 seconds in a day You probably have a reason for using the numbering system you do, but sometimes the best troubleshooting is simply asking if you had the machine plugged in.