Forums
This topic is locked
DateDiff in MySql using PHP
Posted 22 Sep 2006 17:53:42
1
has voted
22 Sep 2006 17:53:42 Fouad Akhtar posted:
i am trying to get a different between 2 dates,
1 date which is in my database and the other is currentdate which is system date
i am not sure if mysql 4 have a datediff function but if some one know please let me know
here how i am getting current date from php page
<?
$displayTime = date("Y-m-d"
// Print to screen
print($displayTime);
?>
here from MySQL
SELECT date_format(DateTime, '%Y-%m-%d ') as Time FROM tbl_comment
now i have both dates in the format of 2006-09-22 , so how could i get the datediff in Days ?????
Replies
Replied 25 Sep 2006 13:43:02
25 Sep 2006 13:43:02 Patrick Woldberg replied:
From the MySQL reference:
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 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.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
So could be used like:
<pre id=code><font face=courier size=2 id=code>SELECT date_format(DateTime, '%Y-%m-%d ') as Time, datediff(DateTime, Now()) as TimeDiff FROM tbl_comment</font id=code></pre id=code>
--------------------------------------------------
Patrick Woldberg
Web Developer at Dynamic Zones
Administrator at DMXzone.com, FLzone.net, FWzone.net and DNzone.com
--------------------------------------------------
<BLOCKQUOTE id=quote><font size=1 face="Verdana, Arial, Helvetica" id=quote>quote:<hr height=1 noshade id=quote>DATEDIFF(expr1,expr2)
DATEDIFF() returns expr1 – expr2 expressed as a value in days from one date to the other. expr1 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.<hr height=1 noshade id=quote></BLOCKQUOTE id=quote></font id=quote><font face="Verdana, Arial, Helvetica" size=2 id=quote>
So could be used like:
<pre id=code><font face=courier size=2 id=code>SELECT date_format(DateTime, '%Y-%m-%d ') as Time, datediff(DateTime, Now()) as TimeDiff FROM tbl_comment</font id=code></pre id=code>
--------------------------------------------------
Patrick Woldberg
Web Developer at Dynamic Zones
Administrator at DMXzone.com, FLzone.net, FWzone.net and DNzone.com
--------------------------------------------------
Replied 25 Sep 2006 23:19:07
25 Sep 2006 23:19:07 Fouad Akhtar replied:
thankyou very much .. this information is very helpful and my problem is solved
Thanks again