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
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
<?php
$a = new \DateTime('2013-12-19');
$b = new \DateTime;
echo $a->diff($b)->days; // => 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();
}
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);