I
I
Ivan Panteleev2014-03-06 21:39:40
MySQL
Ivan Panteleev, 2014-03-06 21:39:40

MySQL - how to know if a new hour has started?

How can MySQL tell if a new hour has started?
And then the option:

DATE(last_limit_error) = CURDATE() AND HOUR(last_limit_error) < HOUR(NOW()) 
OR 
DATE(last_limit_error) < CURDATE()

looks kind of scary.)

Answer the question

In order to leave comments, you need to log in

3 answer(s)
M
Mikhail Rozhkov, 2014-03-06
@Yekver

If the condition is checked at least once an hour, then HOUR(last_limit_error) != HOUR(NOW()) is sufficient.

L
Leonid, 2014-03-07
Gatalsky @mgnstudio

Perhaps if I understand the task correctly, you just need to keep the date and time of the end of the hourly limit in the table, and each time you send a request, compare it with the current date and time, that's all. Additional precomputations are not required, and therefore the load on the server is reduced.
Unless of course you need to prolong it every hour, then it's better to use the method described by @shogunkub

K
Kerman, 2014-03-06
@Kerman

I don't understand people like you. It is working? Works. Does the task? Fulfills. So why are you trying to apply perfectionism to the simplest task?
The fact that the request turned out to be ugly is normal. The DB is not designed for such things. This should be a question for you: why are you asking the base what time it is? Do you have krona (task scheduler)? Or you don't know about event in MySQL?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question