-2

Im trying to work out how many days old is a date in my database.

For example with this date:

$postdate = '2013-12-19';

I want it to work out that it is 1 day old. If it is todays date then it should be 0.

How can i do this in PHP?

Thanks

Exoon
  • 1,513
  • 4
  • 20
  • 35
  • 1
    duplicate of [Php get how many days and hours left from a date](http://stackoverflow.com/questions/7474762/php-get-how-many-days-and-hours-left-from-a-date?rq=1) – Alexander Yancharuk Dec 20 '13 at 13:22
  • 5
    This question should be closed because it has been asked so many many times before and answered so many many times before; and a user who has been a member for as long as this should know how to find those answers – Mark Baker Dec 20 '13 at 13:24

3 Answers3

6
<?php
$a = new \DateTime('2013-12-19');
$b = new \DateTime;

echo $a->diff($b)->days; // => 1
Yoshi
  • 54,081
  • 14
  • 89
  • 103
1

Try this:

try {
    $db_date = new DateTime($postdate);
    $today = new DateTime();
    $interval = $db_date->diff($today);
    echo $interval->format('%R%a days');
} catch (Exception $e) {
    echo $e->getMessage();
}
Patrick Vogt
  • 898
  • 2
  • 14
  • 33
0

Try this

$postdate = '2013-12-20';
$today = date('Y-m-d'); // today date
$diff = strtotime($today) - strtotime($postdate);
echo $days = (int)$diff/(60*60*24);
Bhupendra
  • 248
  • 2
  • 7