Human friendly elapsed time string

Calculated difference in time from now and outputs it in a friendly way. Read more.

php
fork add to bundle source
function time_elapsed_string($datetime) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second', ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); } else { unset($string[$k]); } } $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now'; }

usage

load_function('human_time_elapsed'); time_elapsed_string('2021-12-15 12:00:00'); // 2 years ago
← how to use this in app?
create log ...inout