Of course a cronjob is a start, but even cronjobs might fail or you might have multiple machines for the same database.
Extend the table with a column last_daily_diamond datetime
, which you set when the user gets its diamond, so:
update users set diamonds=diamonds+1, last_daily_diamond = now()
where username='flaschenpost' and last_daily_diamond < now() - interval 24 hour
;
If you mean calendar days and not 24-hour-days, then use not datetime
, but date
column for last_daily_diamond.
update users set diamonds=diamonds+1, last_daily_diamond = curdate()
where username='flaschenpost' and last_daily_diamond <
curdate() ;
If user comes after 10 days and should get 10 diamonds (this decision is up to your customer/boss/yourself!), it gets
update users set diamonds=diamonds+1, last_daily_diamond = last_daily_diamond + interval 1 day
where username='flaschenpost' and last_daily_diamond <
curdate() ;
(if the query itself is send often enough)
or a bit more complicated (not tested yet)
update users set diamonds=diamonds+
(@given := round((unix_timestamp(curdate()) - unix_timestamp(last_daily_diamond))/(24*60*60))),
last_daily_diamond = curdate() + interval @given days
where username='flaschenpost' and last_daily_diamond <
curdate() ;
If you can not extend the users table, you could also create an extra table for this and join to users table.