Jump to content


Photo

Finding the difference in date...


  • Please log in to reply
2 replies to this topic

#1 ChambeRFienD

ChambeRFienD
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 07 April 2006 - 03:30 AM

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.

#2 drewbee

drewbee
  • Members
  • PipPipPip
  • Advanced Member
  • 171 posts
  • LocationColumbus, Ohio

Posted 07 April 2006 - 03:58 AM

[!--quoteo(post=362432:date=Apr 6 2006, 11:30 PM:name=ChambeRFienD)--][div class=\'quotetop\']QUOTE(ChambeRFienD @ Apr 6 2006, 11:30 PM) View Post[/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);
With C you can shoot yourself in the leg. With C++ you can reuse the bullet.

#3 skhale

skhale
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationBoston, MA

Posted 07 April 2006 - 04:08 AM

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






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users