MySQL UNIX_TIMESTAMP Function

MySQL UNIX_TIMESTAMP function is one of the MySQL Date Functions, which returns the seconds from 1970-00-00 00:00:00 UTC. This UNIX_TIMESTAMP function accepts the date in YYMMDD, YYYYMMDD, YYMMDDHHMMSS, or YYYYMMDDHHMMSS formats.

Let us see how to use this UNIX_TIMESTAMP function to get the seconds from 1970 to Date in MySQL with an example.

MySQL UNIX_TIMESTAMP Syntax

The basic syntax of the UNIX_TIMESTAMP() Function in MySQL is as shown below:

UNIX_TIMESTAMP();

UNIX_TIMESTAMP(date);

If you used the Date argument and specified any Date value. Then this function returns the seconds from 1970-00-00 00:00:00 to user given date.

MySQL UNIX_TIMESTAMP function Example

The following examples help you understand the use of the Unix Timestamp function. Here, we are returning the Seconds from 1970 to the different dates.

SELECT UNIX_TIMESTAMP();

SELECT UNIX_TIMESTAMP(NOW());

SELECT UNIX_TIMESTAMP('2019-02-25');
MySQL UNIX_TIMESTAMP Function 1

MySQL UNIX_TIMESTAMP Example 2

Let us see another example of the Unix Timestamp function. Here, we are using NOWCURDATE, along with some addition operation inside this UNIX_TIMESTAMP function. It means we are using the MySQL Date and Times in String format.

SELECT UNIX_TIMESTAMP(NOW() + 20);

SELECT UNIX_TIMESTAMP(CURDATE() + 2);

SELECT UNIX_TIMESTAMP('2018-12-31 23:59:59');
MySQL UNIX_TIMESTAMP Function 2

Let us see another Date Function example of the Unix Timestamp function.

SELECT UNIX_TIMESTAMP('20181231');

SELECT UNIX_TIMESTAMP('20181231235959');
MySQL UNIX_TIMESTAMP Function 3