UNIX_TIMESTAMP() and UTC_TIMESTAMP() in MySQL 8: Explained with examples

Updated: January 26, 2024 By: Guest Contributor Post a comment

Introduction

Handling time in databases can often be trickier than it seems at first glance due to the various ways that time can be stored and manipulated. MySQL provides several helpful functions for doing so, and in this tutorial, we’ll focus on two such functions: UNIX_TIMESTAMP() and UTC_TIMESTAMP(). We’ll discuss how these functions can be used within MySQL 8.0, offer a series of examples to show them in action, and explain the differences between the two.

Understanding UNIX_TIMESTAMP()

The UNIX_TIMESTAMP() function in MySQL is used to convert a date or datetime expression into a Unix timestamp – the number of seconds that have elapsed since the ‘epoch’ (January 1, 1970, 00:00:00 UTC). If called with no argument, the function returns the current date and time as a Unix timestamp.

Basic Example:

SELECT UNIX_TIMESTAMP();

The output will be a Unix timestamp representing the current date and time.

Converting a Specific DateTime:

SELECT UNIX_TIMESTAMP('2023-01-18 10:30:00');

This will output the Unix timestamp of the given date and time.

Understanding UTC_TIMESTAMP()

The UTC_TIMESTAMP() function is slightly different—it returns the current Universal Coordinated Time (UTC) as a value in ‘YYYY-MM-DD HH:MM:SS’ format, or as a ‘YYYYMMDDHHMMSS’ numeric value if used in a numeric context. Unlike UNIX_TIMESTAMP(), it does not convert from a date; it always gives you the current UTC time.

Retrieving Current UTC Time:

SELECT UTC_TIMESTAMP();

This returns the current UTC time in a human-readable format.

Comparative Examples of UNIX_TIMESTAMP() and UTC_TIMESTAMP()

Now let’s take a look at these functions side by side, to clarify the difference between the two.

SELECT UNIX_TIMESTAMP(), UTC_TIMESTAMP();

This will display the current Unix timestamp and the current UTC time respectively, showing how they represent time in different ways.

We can also compare the two by retrieving the Unix timestamp of the current UTC time.

SELECT UNIX_TIMESTAMP(UTC_TIMESTAMP());

This returns the current Unix timestamp, which will match the Unix timestamp from the previous query.

Advanced Usage

Beyond these basic uses, the UNIX_TIMESTAMP() and UTC_TIMESTAMP() functions can also be used in various other contexts.

Calculations with UNIX_TIMESTAMP:

Because Unix timestamps represent time as a number, you can perform calculations on them. For example, to find a timestamp for 24 hours in the past:

SELECT UNIX_TIMESTAMP() - 86400;

Keep in mind that 86400 seconds is the number of seconds in one day. This query deducts one day from the current Unix timestamp.

Or, to create intervals in data retrieval:

SELECT *
FROM your_table
WHERE date_column >= UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY));

This query selects all rows from your_table where the date is within the last 24 hours.

Handling Time Zones

When you’re dealing with times, handling time zones correctly is critical. The UNIX_TIMESTAMP() function always works with UTC time when converting from a datetime value, which should be kept in mind especially if your application works across multiple time zones. On the other hand, UTC_TIMESTAMP() inherently returns time in UTC without any conversions.

Conclusion

In this tutorial, we’ve delved into UNIX_TIMESTAMP() and UTC_TIMESTAMP(), breaking down their uses and showcasing examples ranging from basic to more complex scenarios. By understanding these utility functions in MySQL 8, developers can navigate time-related data with greater ease and precision. As always, practice is the key to mastery, so consider applying these functions in your own database explorations.