public static function elapsed($timestamp, $to = false, $w = 's')
{
// If no $to then now
if (empty($to)) {
$to = time();
}
// Useful to convert request
$useful = array('y' => array('y', 'year'), 'm' => array('m', 'month'), 'd' => array('d', 'day'), 'h' => array('h', 'hour'), 'i' => array('i', 'minute'), 's' => array('s', 'second'));
// Easy search
$w = strtolower($w);
// What you want?
foreach ($useful as $key => $array) {
if (in_array($w, $array)) {
$w = $key;
break;
}
}
// Convesion matrix
$matrix = array('y' => 12 * 30 * 24 * 60 * 60, 'm' => 30 * 24 * 60 * 60, 'd' => 24 * 60 * 60, 'h' => 60 * 60, 'i' => 60, 's' => 1);
// Calculate difference
$elapsed = $timestamp - $to;
// Get days or months or ...
$return = floatval(round($elapsed / $matrix[$w]));
return $return;
}