I need to calculate days between a date I get from the database and the current date.
$upload_date = mysql_query("SELECT Date FROM Setting WHERE ID = $row[ID]");
$current_date = date("Y-m-d");
How do I do it?
If you want to do it in PHP, then use the DateTime class:
$current = new DateTime($current_date);
$db_date = new DateTime($upload_date);
$days = $current->diff($db_date)->days;
Or the oldschool way:
$days = round((strtotime($current) - strtotime($db_date)) /24 /60 /60);
Use SELECT DATEDIFF('new_date', 'old_date');
mysql> SELECT DATEDIFF('2006-04-01','2006-04-01');
+-------------------------------------+
| DATEDIFF('2006-04-01','2006-04-01') |
+-------------------------------------+
| 0 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATEDIFF('2006-04-01','2007-04-01');
+-------------------------------------+
| DATEDIFF('2006-04-01','2007-04-01') |
+-------------------------------------+
| -365 |
+-------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT DATEDIFF('2006-04-01','2005-04-01');
+-------------------------------------+
| DATEDIFF('2006-04-01','2005-04-01') |
+-------------------------------------+
| 365 |
+-------------------------------------+
1 row in set (0.00 sec)
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
form http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
SELECT DATEDIFF( CURDATE(), Date ) FROM ....
and I would not use Date as field name, consider to change this name
$daysDelta = floor((mktime() - strtotime($database['Date']) / 86400);perhaps?