3

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?

3
  • Can you put what format the date is in? Commented Mar 20, 2011 at 15:14
  • They're both YYYY-MM-DD. Commented Mar 20, 2011 at 15:15
  • $daysDelta = floor((mktime() - strtotime($database['Date']) / 86400); perhaps? Commented Mar 20, 2011 at 15:16

6 Answers 6

3
select datediff(curdate(),'2011-03-01');
Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

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

Comments

1

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

Comments

0

You can use this format http://php.net/manual/en/function.date.php and it will do it for you

Comments

0

you can do this in a simple way as below,

$current_date = date("Y-m-d");          // Current date
$db_date = date("Y-m-d");               // Date from your database
$diff = abs(strtotime($current_date) - strtotime($db_date));
$total_days = floor ($diff /  (60*60*24));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.