Last active
November 24, 2021 23:10
-
-
Save ahmadthedev/51820d753b55cdba885ebaa00bfdc499 to your computer and use it in GitHub Desktop.
Post Publish Time in (Year, Month, Week, Day, Hour, Minute, Second)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Time Function | |
function time_elapsed_string($datetime, $full = false) { | |
$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]); | |
} | |
} | |
if (!$full) $string = array_slice($string, 0, 1); | |
return $string ? implode(', ', $string) . ' ago' : 'just now'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment