function timeSpent($date_created, $endingDay)
{
$diff = abs(strtotime($date_created) - strtotime(date("Y-m-d H:i:s")));
$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));
$minuts = 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 - $minuts * 60));
$week = floor($days / 7);
$day = $days % 7;
if ($week < 1 && $week != 0) {
return $days . " days - " . $hours . " hrs - " . $minuts . "- min" . $week;
} elseif ($week == 0) {
if ($days != 0) {
return $days . "days " . $hours . "hours ago";
} elseif ($hours != 0) {
return $hours . "hours " . $minuts . "minute ago";
} elseif ($minuts) {
return $minuts . "minute " . $seconds . "seconds ago";
} else {
return "few seconds ago";
}
} else {
$stamp = strtotime($date_created);
return date("F d Y", $stamp);
}
}
}
i am using this function it doesn't work properly.
One error I notice is that it can be possible to have 1 month and 0 weeks like if you were to enter April 15th. Which would result in only outputting the days and hours. Change the following.
} elseif ($week == 0) {
to
} elseif ($week == 0 && $months == 0 && $years == 0) {
This could be causing your problem depending on the date you entered.
精彩评论