Unix timestamp to human readable date and vice versa

Unix timestamp is the number of seconds since january 1, 1970 00:00:00 UTC. PHP strtotime() and date() functions are used to get unix timestamp and human readable date respectively. Setting different timezones while converting date or timestamp differs the final results.

Current Epoch Unix Time is: 1643418588

Date to epoch unix timestamp converter


Unix timestamp to date converter

How do we get timestamp in PHP?

Using strtotime() function we can get unix timestamp. it is also very important to note that strtotime() requires a default timezone otherwise it will generate timestamp in server timezone which could be misleading.

PHP Code Example:
// set default timezone
//define date and time
$date date("d M Y H:i:s");
// output
echo strtotime($date);
How do we get readable date from timestamp?

We can use date() function to generate readable date.

PHP Code Example:
// set default timezone
//define unix timestamp
$timestamp 1456778973;
// output
echo date('d M Y H:i:s',$timestamp);
Date to timestamp in other languages
MySQL SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2072 12:00AM', '%M %d %Y %h:%i%p'))
SQLite SELECT strftime('%s', 'now')
PostgreSQL SELECT to_timestamp('20/8/2072 14:52:49', 'DD/MM/YYYY hh24:mi:ss')::timestamp
Go time.Now().Unix()
JavaScript Math.floor(new Date().getTime()/1000.0)
Lua epoch = os.time([date])
Java long epoch = System.currentTimeMillis()/1000
Ruby str = "2072-05-27T07:39:59Z"

Timestamp to date conversion in other languages
MySQL select DATE_FORMAT(FROM_UNIXTIME(your_timestamp_col), '%e %b %Y') AS date FROM your_table
SQLite SELECT strftime('%d - %m - %Y ', datetime(your_timestamp_col, 'unixepoch')) FROM your_table;
PostgreSQL select to_timestamp(epoch_column)::date
Go currentSeconds := time.Now().Unix()
fmt.Println(“Time in seconds: “, currentSeconds)
JavaScript new Date (timestamp);
Lua local ts = os.time()
print(os.date('%Y-%m-%d %H:%M:%S', ts))
Java Timestamp stamp = new Timestamp(System.currentTimeMillis());
Date date = new Date(stamp.getTime());
Ruby Time.at(timestamp)