how to get the time difference between 2 values while if second value is past the current time?
i use below code:
function time_diff($date2, $date1, $format){
$diff = abs(strtotime($date2) - strtotime($date1));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
$hours = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24) / (60*60));
$minutes = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60)/ 60);
$seconds = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24 - $days*60*60*24 - $hours*60*60 - $minutes*60));
if ($format == 'year'){
$i = $years;
}elseif ($format == 'month'){
$i = $months;
}elseif ($format == 'day'){
$i = $days;
}elseif ($format == 'hour'){
$i = $hours;
}elseif ($format == 'minutes'){
$i = $minutes;
}elseif ($format == 'second'){
$i = $seconds;
}
return $i;
}
i want to get time difference between (saved time in database) and (current time).
everything is fine, but when (saved time in database) passes and its smaller than (current time), this code return value but without negative sign.
i mean if time difference is 2 day, it return 2 while it should return (-2) or somethings like that cuz (saved time in database) passes and its smaller than (current time).