All this code does is create two DateTime objects: one representing two years ago, one representing your initialize time. It then compares them (DateTime objects are comparable). If $initial_date
is greater than two years ago it just assigns that date in the format you requested to $time_elapse
. Otherwise your code runs as usual.
<?php
$initial_time="946681200";//the unix timestamp is for Dec 31, 1999
$two_years_ago = new DateTime('-2 years');
$initial_date = new DateTime('@' . $initial_time);
if ($initial_date < $two_years_ago) {
$time_elapse = $initial_date->format('d-m-Y');
}
else {
$periods = array("sec", "min", "hr", "day", "week", "month", "yr");
$lengths = array("60","60","24","7","4.35","12","10");
$span = time() - $initial_time;
$tense = "ago";
for($j = 0; $span >= $lengths[$j] && $j < count($lengths)-1; $j++)
{
$span /= $lengths[$j];
}
$span = round($span);
if($span != 1) {
$periods[$j].= "s";
}
$time_elapse = $span.' '.$periods[$j] ;
}
?>