Skip to content

Instantly share code, notes, and snippets.

@tavinus
Last active September 23, 2020 20:25
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 tavinus/2a20d4de836153159256af220e2b6673 to your computer and use it in GitHub Desktop.
Save tavinus/2a20d4de836153159256af220e2b6673 to your computer and use it in GitHub Desktop.
Convert an epoch timestamp to a human readable format

How do I convert an epoch timestamp to a human readable format?

https://unix.stackexchange.com/questions/2987/how-do-i-convert-an-epoch-timestamp-to-a-human-readable-format-on-the-cli

I think there's a way to do it with date but the syntax eludes me (other ways welcome).

On *BSD:

date -r 1234567890

On Linux (specifically, with GNU coreutils ≥5.3):

date -d @1234567890

With older versions of GNU date, you can calculate the relative difference to the UTC epoch:

date -d '1970-01-01 UTC + 1234567890 seconds'

If you need portability, you're out of luck. The only time you can format with a POSIX shell command (without doing the calculation yourself) line is the current time. In practice, Perl is often available:

perl -le 'print scalar localtime $ARGV[0]' 1234567890

PHP date()

echo(date("Y-m-d h:i:sa",1234567890));

PHP DateTime

$datetime = new DateTime("@1234567890");
echo $datetime->format('d-m-Y H:i:s');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment