I want this Code to do query just once in a day means after it happened the code will be hidden or disabled some how, is that possible ? thanks :)
$time = date("G");
if($time >= 12 && (isset($_SESSION['UserName']))){
mysql_query("Update users set diamonds = diamonds+1 where username = 'NaiTreNo'");
}
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.
The proper way to do it, would be to use a cron job in linux and execute your php script there once a day.
But if you really want to keep it in your php code you can pick one time a day and make an if statement for checking the time