PHP date_ago function!
Thought I posted this labourious piece of code that changes a unix timestamp into a “xx <seconds|minutes|hours|days|weeks|months> ago” date format.
<?php
function date_ago($time) {
if ($time < 60) {
$num = $time;
$unit = 'second';
}
elseif ($time < 3600) {
$num = floor($time / 60);
$unit = 'minute';
}
elseif ($time < 86400) {
$num = floor($time / 3600);
$unit = 'hour';
}
elseif ($time < 604800) {
$num = floor($time / 86400);
$unit = 'day';
}
elseif ($time < 2419200) {
$num = floor($time / 604800);
$unit = 'week';
}
elseif ($time < 29030400) {
$num = floor($time / 2419200);
$unit = 'month';
}
return $num . ' ' . $unit . ($num > 1 ? 's' : '');
}
I wonder where I’ve seen that before
Geez! I wonder…
I hope this snippet is MIT or BSD Licensed!