Jump to content

Archived

This topic is now archived and is closed to further replies.

ChambeRFienD

Finding the difference in date...

Recommended Posts

How would I go about finding the difference between a date stored in the 'DATE' type in a MySQL database and the current date.. Just looking for how many days there are between the current date and what is stored in the database.

Share this post


Link to post
Share on other sites
[!--quoteo(post=362432:date=Apr 6 2006, 11:30 PM:name=ChambeRFienD)--][div class=\'quotetop\']QUOTE(ChambeRFienD @ Apr 6 2006, 11:30 PM) [snapback]362432[/snapback][/div][div class=\'quotemain\'][!--quotec--]
How would I go about finding the difference between a date stored in the 'DATE' type in a MySQL database and the current date.. Just looking for how many days there are between the current date and what is stored in the database.
[/quote]

is it in timestamp?

try this:

// mysql timestamp from db
$old_timestamp = $row['date'];


// Time in seconds from then to right now
$elapsed_seconds = time() - $old_timestamp;

// divide by 60 for minutes, 60 for hours, then 24 for days, using floor to make an integer
$elapsed_days = floor(($elapsed_seconds / 60) / 60 / 24);

Share this post


Link to post
Share on other sites
[quote]
DATEDIFF(expr,expr2)

DATEDIFF() returns the number of days between the start date expr and the end date expr2. expr and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

mysql> SELECT DATEDIFF('1997-12-31 23:59:59','1997-12-30');
-> 1
mysql> SELECT DATEDIFF('1997-11-30 23:59:59','1997-12-31');
-> -31

DATEDIFF() was added in MySQL 4.1.1
[/quote]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.