Sign in to follow this  
Cygnus_X

[web] Number of Days between two Dates

Recommended Posts

Cygnus_X    359
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.

Share this post


Link to post
Share on other sites
Deranged    668
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'.

Share this post


Link to post
Share on other sites
rfterdarc    139
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/

Share this post


Link to post
Share on other sites
Cygnus_X    359
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 :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this