[web] Number of Days between two Dates

Started by
2 comments, last by Cygnus_X 18 years, 4 months ago
If I have a mysql time/date stamp (in the format of YYYY-MM-DD HH:MM:SS), and I want to see how many days have passed between todays current date and the time stamp (The time part of the stamp doesn't need to be evaulated, just the date). Is there any easy way of doing this in PHP? I've googled it a few times, and I started running into functions that required me to check for leap year.... so I've decided to ask around before attempting to modify such a function for my needs. Anyways, thanks in advance.
Advertisement
I've always made it habit to check for leap year, otherwise your value could be a day off every fourth year, since 1970(unix time) or otherwise you should still do it as a 'just in case'.
mySQL has TIMEDIFF( time1, time2 ) function. this would best to use

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

to convert from mysql timestamp to unix timestamp without mysql:
http://devcorner.georgievi.net/articles/php/phpdatetotimestamp/
Ok.. I found my answer. The trick is to do it like this:

SELECT *, DateDiff(LastLogin, Now( )) AS Diff FROM UserStats where Username = '$UserName';

Where LastLogin is a column containing the date/time of the last login. The 'Diff' column will contain the difference in terms of number of days. Very useful :)

This topic is closed to new replies.

Advertisement