Created
September 13, 2018 16:33
-
-
Save mrtonyhuynh/73e64ba8a5da690a66bdc759ef6b60e8 to your computer and use it in GitHub Desktop.
PHP: 'Time Ago' helper function. Useful for Twitter feeds
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
| //helper function for 'x hours/minutes/seconds ago' - should be passed a time in php time format | |
| function time_elapsed_string($ptime) | |
| { | |
| $etime = time() - $ptime; | |
| if ($etime < 1) | |
| { | |
| return '0 seconds'; | |
| } | |
| $a = array( 12 * 30 * 24 * 60 * 60 => 'year', | |
| 30 * 24 * 60 * 60 => 'month', | |
| 24 * 60 * 60 => 'day', | |
| 60 * 60 => 'hour', | |
| 60 => 'minute', | |
| 1 => 'second' | |
| ); | |
| foreach ($a as $secs => $str) | |
| { | |
| $d = $etime / $secs; | |
| if ($d >= 1) | |
| { | |
| $r = round($d); | |
| return $r . ' ' . $str . ($r > 1 ? 's' : '') . ' ago'; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment