Skip to main content

Command Palette

Search for a command to run...

Script For Converting To/From UNIX Time

PHP Command Line Script

Published
1 min read
Script For Converting To/From UNIX Time
H

I am a developer from Malaysia. I work with PHP most of the time, recently I fell in love with Go. When I am not working, I will be ballroom dancing :-)

I wrote an intuitive command line script for converting UNIX time to human-readable format and vice versa.

When I need to convert UNIX time, a quick Google search will yield solutions like this:

# Works on Mac OS but not Linux
date -r 1624032013

# Works on Linux but not Mac OS
date -d @1624032013

# Get current time UNIX timestamp
date +%s

I find "-r", "-d", "+%s" hard to remember. So I wrote a PHP script to simplify it. Save it as /usr/bin/unixtime and this is how I use it:

# Get current UNIX timestamp
unixtime

# Covert to UNIX time
unixtime "2021-12-24 23:59:59"

# Convert UNIX time to human readable time
unixtime 1624032013

# Get hints on how to use this script
unixtime help

Source code:

More from this blog

Hong's Tech Blog

110 posts

The blog is older than you know. I prefer counting from the emergence of one integral anomaly to the emergence of the next, in which case this is the forth version.