MySQL Forums
Forum List  »  PHP

Re: difference of dates
Posted by: Jonathan Stephens
Date: June 26, 2005 07:47PM

How are you actually storing the datefield values? Is it a DATE or DATETIME column? What version of MySQL are you using?

You can't simply subtract DATE or DATETIME values from one another. If datefield is actually a DATE or DATETIME, and you're using MySQL 4.1.1 or newer, you can get the difference with this:

SELECT DATEDIFF(datefield, NOW()) AS difference;

DATEDIFF() returns the difference between two DATE and/or DATETIME values as a number of days. In this case, the result will be positive if the date's in the future, and negative if it's in the past:

mysql> SELECT NOW();
+---------------------+
| NOW() |
+---------------------+
| 2005-06-27 11:29:18 |
+---------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2005-06-01', NOW()) AS difference;
+------------+
| difference |
+------------+
| -26 |
+------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2005-07-01', NOW()) AS difference;
+------------+
| difference |
+------------+
| 4 |
+------------+
1 row in set (0.00 sec)

Otherwise, you might want to look at http://dev.mysql.com/doc/mysql/en/date-and-time-functions.html or provide additional info.

Jon Stephens
MySQL Documentation Team @ Oracle
Orlando, Florida, USA

MySQL Dev Zone
MySQL Server Documentation
Oracle

Options: ReplyQuote


Subject
Written By
Posted
June 26, 2005 01:21PM
Re: difference of dates
June 26, 2005 07:47PM
June 27, 2005 08:29AM
June 27, 2005 08:55AM
June 27, 2005 11:58AM


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.