1. <?php
  2.  
  3. //connect to database
  4. //....
  5.  
  6. //prepare MySQL query
  7. $sql = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
  8.  
  9. //execute query and handle errors
  10. //...
  11.  
  12. //optinal reporting on e-mail
  13. //...
  14.  
  15. //end
  16. ?>
  17.  
  18. 0 0 * * * /usr/bin/php -f /path/to/my_update_script.php > /dev/null
  19.  
  20. CREATE TABLE `php_cron` (
  21. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  22. `last_ts` datetime DEFAULT NULL,
  23. PRIMARY KEY (`id`)
  24. );
  25.  
  26. INSERT INTO `php_cron` (`id`, `last_ts`) VALUES (1,'2012-08-10 00:00:00');
  27.  
  28. <?php
  29.  
  30.  
  31. //connect to database
  32. //....
  33.  
  34. //get time difference in seconds from last execution
  35. $sql1 = "SELECT TIME_TO_SEC(TIMEDIFF(NOW(), last_ts)) AS tdif FROM php_cron WHERE id=1";
  36. $res1 = mysql_query($sql1) or die("[1] MySQL ERROR: ".mysql_error());
  37. $dif = mysql_fetch_assoc($res1['tdif']);
  38.  
  39.  
  40. if ($dif >= 86400) { //24h
  41.  
  42. //following code will run once every 24h
  43.  
  44. //update user's page rank
  45. $sql2 = "UPDATE myTable SET `user-rank` = `user-rank` + 10";
  46. mysql_query($sql2) or die("[2] MySQL ERROR: ".mysql_error());
  47.  
  48. //update last execution time
  49. $sql3 = "UPDATE php_cron SET last_ts = NOW() WHERE id=1";
  50. mysql_query($sql3) or die("[3] MySQL ERROR: ".mysql_error());
  51.  
  52. }
  53.  
  54. ?>
  55.  
  56. crontab -e
  57.  
  58. 0 0 * * * php /var/www/myquery.php