I have the following:
$dateStart = get_post_meta( get_the_ID(), 'startdate' , true);
$dateStart = DateTime::createFromFormat('d/m/Y h:i:s', $dateStart);
$dStart = $dateStart->format('d/m/y');true);
$dEnd = date("d/m/y");
echo $dStart .'-'.$dEnd;
This outputs 12/06/15-28/10/15
but for the life of me I can't work out how to get the difference in days between the two dates.
Any advice? I've tried a few things but they error out each time.
Thanks
The most secure way I found to compute the number of days between a date DateTime $a
and another on DateTime $b
is to rely on their timestamps at midnight:
protected static function diffDays(DateTime $a, DateTime $b)
{
return round(
($a->setTime(0, 0, 0)->getTimestamp() - $b->setTime(0, 0, 0)->getTimestamp())
/ 86400 // 60 * 60 * 24 = 86400s in 24h
);
}
Work very well for me :)
Note: I use hard coded value 86400
in order to reduce useless processing time calculating 60 * 60 * 24