feelay Posted February 13, 2008 Share Posted February 13, 2008 Hello, Again. I am having a little problem with a time script. It is using the time() function, but I don't know how to use that function (I didn't create this script. a guy made it for me). The problem is, that the value is not updating in the database(it should update every 12 minutes, or every minute,depending on what i write in a place..) I guess that the fault is in the code somewhere.. but it is not in the queries.. I've tested them.. I can't find anything, but as I said, i don't know how this function works.. so can anyone please try to help me figure out whats wrong in this code: <?php $multiplier = 1 / 5; $frequency = 3600 * $multiplier; $increment = 5 * $multiplier; $current_time = time(); // get the current server time in second $last_update_time = mysql_result(mysql_query("SELECT hptime FROM characters"), 0); // This could be anywhere even in a separate text file! $time_difference = $current_time - $last_update_time; $hours_passed = $time_difference / $frequency; // convert sec to min then to hours --> 3600 second / 60 = 60 minutes / 60 = 1 hour $full_hours_passed = floor($hours_passed); // round the number of decimal hours down to the nearest whole hour $remaining_seconds = $time_difference % $frequency; // number of sec left when time difference is divided by 3600 sec --> 7 % 3 = 1 and 6 % 3 = 0 if($full_hours_passed > 0){ $new_update_time = $current_time + $remaining_seconds; $hp_increase = $full_hours_passed * $increment; mysql_query("UPDATE `characters` SET `temphealth` = CASE WHEN `temphealth` + $hp_increase <= `maxhp` THEN `temphealth` + $hp_increase ELSE `maxhp` END WHERE `temphealth` > `maxhp`"); mysql_query("UPDATE characters SET hptime = $new_update_time"); } ?> Link to comment https://forums.phpfreaks.com/topic/90956-something-is-wrong-with-this-time-script/ Share on other sites More sharing options...
Isityou Posted February 13, 2008 Share Posted February 13, 2008 The only way this would work if something executed the script every some interval. I suggest you use a cron job to execute the script every interval. Link to comment https://forums.phpfreaks.com/topic/90956-something-is-wrong-with-this-time-script/#findComment-466169 Share on other sites More sharing options...
feelay Posted February 13, 2008 Author Share Posted February 13, 2008 I don't like cronjobs.. There must be another way.. Cron jobs can be useful at times, but here they would be too much. You would have a script that runs every sixty seconds and updates all the players. Sure, for only several players it is OK, but just imagine updating several thousands of players - it would definitely put immense load on the server (unnecessary load, that is). Quote from another forum.. Link to comment https://forums.phpfreaks.com/topic/90956-something-is-wrong-with-this-time-script/#findComment-466173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.