There is an hour row in the users table, the default value is 0.

After the function is executed, the hour value becomes 1

How to make it so that, 4 hours after the value became 1, it again changed to 0?

  • one
    It is better not to use the field with the values ​​0 and 1, but to write in the record the date / time of the last operation performed. Then it will be easy to check that 4 hours have already passed. hour < now()-interval 4 hour - Mike
  • I didn’t quite understand what the code was at the end. - Timofreed 5:34 pm
  • Well, this is part of either where or select itself. depending on the desired effect. compare the field with the current date - 4 hours for example - Mike

1 answer 1

As I understand you want to make it so that the user can perform a function once every 4 hours, you can do this: After performing the function, we record (the current time + the time interval we need (in this case, 60 * 60 * 4 seconds) time() + 14400 to the database and when we try to call this function we compare the time to time , if the current time is less than the time in the table, then we refuse to execute.

  • I don’t know much about php and mysql) If it’s not difficult, could you write it immediately with ready-made code? - Timofreed pm
  • Here is the principle, you only need to figure out the choice of the value from the database and your function sandbox.onlinephpfunctions.com/code/… - nameisusselesinfo
  • one
    @nameisusselesinfo No need to store time in the database in seconds. it must always be stored in the form of datetime. Then it will not be necessary to calculate seconds, but it will be possible to write now() + interval 4 hour instead of time () + 14400, in addition, MySQL will make it much more efficient than php - Mike