Skip to content

Instantly share code, notes, and snippets.

@jkoop
Last active March 23, 2022 14:19
Show Gist options
  • Save jkoop/7c3ae35681e167b0b4294aca97bf8586 to your computer and use it in GitHub Desktop.
Save jkoop/7c3ae35681e167b0b4294aca97bf8586 to your computer and use it in GitHub Desktop.
Human-readable date/time for use in stuff like directory lists
<?php
// This is intended for use in stuff like directory lists
// 1234567890 unix time (input)
// 23:31:30 if today
// 13d 23:31 if this month
// Feb 13 23h if this year
// '09 Feb 13 if farther away
function humanReadableDateTime(int $time): string {
if (date('Y-m-d', $time) == date('Y-m-d')) return date('H:i:s', $time);
if (date('Y-m', $time) == date('Y-m')) return date('d\d H:i', $time);
if (date('Y', $time) == date('Y')) return date('M d H\h', $time);
return date("'y M d", $time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment