cls
cls

Reputation: 561

UNIX_TIMESTAMP() repeated more than once in MySQL request

I need to get current date (and time 00:00:00) epoch timestamp in MySQL request. I want to do this with math expression: (UNIX_TIMESTAMP() - (UNIX_TIMESTAMP() % 86400)).

But this calls UNIX_TIMESTAMP() more than once; so the question is - is there a possibility that two UNIX_TIMESTAMP() functions return different time? What is the workaround in this case?

Or maybe MySQL executes UNIX_TIMESTAMP() once, and replaces all other calls with current timestamp and no workaround needed?

Upvotes: 1

Views: 294

Answers (3)

pilcrow
pilcrow

Reputation: 58534

You may simply use UNIX_TIMESTAMP(), which will return the start time of the current query.

This isn't explicitly documented — the docs make general reference to the stability of "Functions that return the current date or time ..." — but it is so.

Upvotes: 1

Paul Dixon
Paul Dixon

Reputation: 300825

You could use UNIX_TIMESTAMP(NOW()), because NOW() returns the timestamp when the statement began to execute, so it would behave consistently across multiple calls in the same statement.

NOW() returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.)

Upvotes: 3

i486
i486

Reputation: 6564

SELECT @ut:=UNIX_TIMESTAMP(), (@ut - (@ut % 86400)) AS ep;

Upvotes: 2

Related Questions