Skip to content

Instantly share code, notes, and snippets.

@maxpatiiuk
Last active April 23, 2023 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxpatiiuk/54b7d6648a60a21a635f902de7a5d6b4 to your computer and use it in GitHub Desktop.
Save maxpatiiuk/54b7d6648a60a21a635f902de7a5d6b4 to your computer and use it in GitHub Desktop.
Converts a given timestamp from the past into a useful human form
/*
* This script is now deprecated. Find a much better version at
* https://github.com/specify/specify7/blob/393538b081eb797beb502204cdea9311179361f6/specifyweb/frontend/js_src/lib/components/Atoms/Internationalization.ts#L129-L172
*
* Along with tests:
* https://github.com/specify/specify7/blob/393538b081eb797beb502204cdea9311179361f6/specifyweb/frontend/js_src/lib/components/Atoms/__tests__/internationalization.test.ts#L64-L120
*/
function unix_time_to_human_time($time){
$time_passed = time()-$time;
if($time_passed<60)
$result = $time_passed.' seconds ago';
elseif($time_passed<3600)
$result = intval($time_passed/60).' minutes ago';
elseif($time_passed<86400)
$result = intval($time_passed/3600).' hours ago';
else
$result = intval($time_passed/86400).' days ago';
return preg_replace('/^(1 \w+)s( ago)/','$1$2',$result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment