Image for post
Image for post

Working with Unix timestamps in MySQL and Postgres

The following are examples of how to work with unix timestamps in both MySQL/MariaDB and Postgres.

MySQL/MariaDB

To get the current date and time in MySQL (based on the timezone of the server):

If you just want the date:

To convert a unix timestamp to a readable date/time format use:

Postgres

To get both the current date and time in unix timestamp format to the nearest second (trunc) based on the time zone of the server:

Conversely to convert a unix timestamp to a Postgres timestamp use:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store