PHP Get days difference between a date and the date today (Now)

Jin Pards picture Jin Pards · Oct 25, 2015 · Viewed 14.7k times · Source

I can't seem to get this to work. I have tried from the samples online but there wasn't one the is exactly what I needed. Basically I want to be able to display the number of days that passed from the given date. My sample below is a combined HTML and PHP, I had to do it this way for some reasons.

<?php
$OldDate = strtotime($row['DateSigned']);
$NewDate = date('M j, Y', $OldDate);
?>

<b>Date Signed:</b> <?php echo $NewDate; ?>
<b>Days Since Signed:</b> <?php echo date_diff(strtotime($NewDate),Date("y/m/d")); ?>

This seem to fail.Date("y/m/d") is the date today. Can you tell me what went wrong?

Answer

Akhil VL picture Akhil VL · Oct 25, 2015

This will work:

<?php
$OldDate = strtotime("2015-10-21");
$NewDate = date('M j, Y', $OldDate);
$diff = date_diff(date_create($NewDate),date_create(date("M j, Y")));
?>

<b>Date Signed:</b> <?php echo $NewDate; ?>
<b>Days Since Signed:</b> <?php echo $diff->format('%R%a days'); ?>