Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Time to "Number of Days Ago"

9:47 pm on Jul 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 20, 2009
posts: 56
votes: 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

10:48 pm on July 28, 2012 (gmt 0)

Junior Member

5+ Year Member

joined:Dec 20, 2009
posts: 56
votes: 0


No replies but I got it figured out thanks to a post at StackOverflow:


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!
11:15 pm on July 28, 2012 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
votes: 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".
8:18 pm on July 31, 2012 (gmt 0)

New User

5+ Year Member

joined:July 31, 2012
votes: 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.

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members