A better way to get a nice time-format (1 year ago, 2 months until) without all the trailing months, days, hours, minutes, seconds in the result is by using the DateTime format and using the date_diff function as they both does most of the heavy lifting for you
Function below as example
<?php
function getTimeInterval($date) {
$date = new DateTime ($date);
$now = date ('Y-m-d H:i:s', time()); $now = new DateTime ($now);
if ($now >= $date) {
$timeDifference = date_diff ($date , $now);
$tense = " ago";
} else {
$timeDifference = date_diff ($now, $date);
$tense = " until";
}
$period = array (" second", " minute", " hour", " day", " month", " year");
$periodValue= array ($timeDifference->format('%s'), $timeDifference->format('%i'), $timeDifference->format('%h'), $timeDifference->format('%d'), $timeDifference->format('%m'), $timeDifference->format('%y'));
for ($i = 0; $i < count($periodValue); $i++) {
if ($periodValue[$i] != 1) {
$period[$i] .= "s";
}
if ($periodValue[$i] > 0) {
$interval = $periodValue[$i].$period[$i].$tense; }
}
if (isset($interval)) {
return $interval;
} else {
return "0 seconds" . $tense;
}
}
getTimeInterval("2016-05-04 12:00:00"); getTimeInterval("2017-12-24 12:00:00"); ?>