sanchitkhanna26
sanchitkhanna26

Reputation: 2233

Difference between UNIX_TIMESTAMP and NOW() in MySQL

I have a blog where users can comment. I insert the time at which they posted a comment using NOW() and then use date('j M Y', stored timestamp) to show the time at which they posted.

I want to know does NOW() return the locatime of the end user or the localtime at my server. Is it better suited to use UNIX_TIMESTAMP than NOW() to calculate the localtime at which users posted a comment.

Upvotes: 9

Views: 31478

Answers (3)

user2001117
user2001117

Reputation: 3777

MySQL UNIX_TIMESTAMP() returns a Unix timestamp in seconds since '1970-01-01 00:00:00' UTC as an unsigned integer if no arguments are passed with UNIT_TIMESTAMP().

When this function used with date argument, it returns the value of the argument as an unsigned integer in seconds since '1970-01-01 00:00:00' UTC.

Argument may be a DATE, DATETIME,TIMESTAMP or a number in YYYYMMDD or YYMMDD.

Note : Since UNIX_TIMESTAMP() works on current datetime, your output may vary from the output shown.

NOW() returns the current date and time.

SELECT NOW(), UNIX_TIMESTAMP(NOW());
+---------------------+-----------------------+
| NOW()               | UNIX_TIMESTAMP(NOW()) |
+---------------------+-----------------------+
| 2011-10-03 10:22:37 |            1317666157 |
+---------------------+-----------------------+

Upvotes: 8

Ja͢ck
Ja͢ck

Reputation: 173522

The function NOW() generates a formatted date-time string, determined by the time zone of your MySQL server.

However, it would be better to store times using UNIX_TIMESTAMP(), which is expressed in GMT. Doing so makes it easier to format it according to the country of a visitor (e.g. using JavaScript).

If you still want to use DATETIME columns, you can store times using UTC_TIMESTAMP() (it formats a date like NOW() but expresses it in UTC); it should more or less work the same in all other aspects.

Upvotes: 9

Álvaro González
Álvaro González

Reputation: 146350

Let's see what the manual has to say about NOW():

Returns the current date and time as a value in 'YYYY-MM-DD HH:MM:SS' or YYYYMMDDHHMMSS.uuuuuu format, depending on whether the function is used in a string or numeric context. The value is expressed in the current time zone.

... and UNIX_TIMESTAMP():

If called with no argument, returns a Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) as an unsigned integer. If UNIX_TIMESTAMP() is called with a date argument, it returns the value of the argument as seconds since '1970-01-01 00:00:00' UTC. date may be a DATE string, a DATETIME string, a TIMESTAMP, or a number in the format YYMMDD or YYYYMMDD. The server interprets date as a value in the current time zone and converts it to an internal value in UTC.

So, to begin with, they return different things: a proper date versus an integer.

You actually need to get three features:

  1. Store all dates in the same format (either UTC or the server's time zone)
  2. Obtain user's time zone
  3. Display stored date in user's time zone

The Date and Time functions chapter offers a summary of available functions. If you want to store dates in UTC you'd go for UTC_TIMESTAMP(). If you want to use server's time zone you can use NOW(). And there's CONVERT_TZ() to make conversions.

MySQL, however, won't help you with point #2. You need to either ask the user or use JavaScript to read user's clock and send it to the server so you can guess (if you don't ask you'll always need to guess because there're normally several time zones that share the same time in a given instant).

Upvotes: 3

Related Questions