MySQL Forums
Forum List  »  PHP

Re: difference between to dates
Posted by: Nick Roper
Date: April 28, 2005 03:10PM

Hi Trudy,

Where are you doing the calculation - as part of the query, or once the data are back in PHP ?

If it's in the query, then the following should give you the number of days between the date and today:

SELECT TO_DAYS(mydate) - TO_DATE(NOW()) AS interval from mytable;

If you have retrieved the date from the table into a variable in PHP, then you can use:

<?php

$sql = ("SELECT mydate FROM mytable WHERE .....");
$result = mysql_query($sql);

while ($row = mysql_fetch_assoc($result)) {

$db_date = $row[mydate];

// generate a unix timestamp for the date from the database
$db_date = strtotime($db_date);

// generate a unix timestamp for today at 00:00:00 hrs
$today = mktime(0,0,0);

// calc the interval and convert from seconds to days
// use abs() to convert to an unsigned value
$interval = abs($db_date - $today)/(24*60*60);

echo $interval;

}

Cheers,


Nick

--
Nick Roper

Options: ReplyQuote


Subject
Written By
Posted
Re: difference between to dates
April 28, 2005 03:10PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.