Skip to content

Instantly share code, notes, and snippets.

View jersonmartinez's full-sized avatar
💻
I enjoy the code!

Jerson Martínez jersonmartinez

💻
I enjoy the code!
View GitHub Profile
@jersonmartinez
jersonmartinez / elapsed-time.php
Created February 9, 2020 16:45 — forked from james2doyle/elapsed-time.php
PHP elapsed time function which takes in a timestamp and spits out a "timeago" sentence.
<?php
function elapsed_time($timestamp, $precision = 2) {
$time = time() - $timestamp;
$a = array('decade' => 315576000, 'year' => 31557600, 'month' => 2629800, 'week' => 604800, 'day' => 86400, 'hour' => 3600, 'min' => 60, 'sec' => 1);
$i = 0;
foreach($a as $k => $v) {
$$k = floor($time/$v);
if ($$k) $i++;
$time = $i >= $precision ? 0 : $time - $$k * $v;
$s = $$k > 1 ? 's' : '';