Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
function time_elapsed_string($datetime, $full = false)
{
date_default_timezone_set('Asia/Jerusalem');
$now = new DateTime();
$ago = new DateTime($datetime);
$diff = $now->diff($ago);
$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;
$string = [
"y" => "year",
"m" => "mounth",
"w" => "week",
"d" => "day",
"h" => "hour",
"i" => "minute",
"s" => "sencond",
];
foreach ($string as $k => &$v) {
if ($diff->$k) {
$v = $diff->$k . " " . $v . ($diff->$k > 1 ? "" : "");
} else {
unset($string[$k]);
}
}
if (!$full) {
$string = array_slice($string, 0, 1);
}
return $string ? implode(", ", $string) . " ago" : "just time";
}
$dateok = "2022-05-15 11:12:16"; // year-mounth-date hours:minute:second
echo time_elapsed_string($dateok);