MasterACE14 Posted October 4, 2007 Share Posted October 4, 2007 Evening, I have a cronjob that runs a PHP file every hour. I have made 2 functions, 1 to workout how long it has been since the last time the cornjob has run, and 1 to workout how long it is until the next cronjob. But I believe I have stuffed up somewhere. here is the functions: <?php // Workout the time since the last turn change function lastturn($time){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); } // Workout the time until the next turn change function nextturn(){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "SELECT `lastturn` FROM `cf_info`"; $lastturntime = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); // grab time since last turn change $lastturnchange = $lastturntime; // get the current time $currenttime = time(); // get the difference between last turn change and the current time $dif = $currenttime - $lastturnchange; // workout how many minutes until the next turn : part 1 $nextturnmin = $dif/60; // workout how many minutes until the next turn : part 2 $nextturn = 60 - $nextturnmin; // if the time until the next turn is less then zero, set the time till next turn to 0 if ($nextturnmin < 0) { $nextturnmin = 0; } // return the time until next turn change return $nextturnmin; } here is the PHP file that the cronjob runs: <?php // cron job include_once '/home/ace/public_html/conflictingforces/functions.php'; lastturn(time()); nextturn(); ?> and here is what is being displayed when I echo nextturn(); 19858165.4666 and it isn't doing anything to the cf_info table. Regards ACE Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/ Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 you missing fetch from the MySql Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361549 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 still not showing anything in the database. And I always stuff up the mysql_fetch :-\: <?php // Workout the time since the last turn change function lastturn($time){ $rs = mysql_connect( "localhost", "ace_ACE", "shadow69" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $rss = mysql_fetch_array( $rs ); } // Workout the time until the next turn change function nextturn(){ $rs = mysql_connect( "localhost", "ace_ACE", "shadow69" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "SELECT `lastturn` FROM `cf_info`"; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $lastturntime = mysql_fetch_array( $rs ); // grab time since last turn change $lastturnchange = $lastturntime; // get the current time $currenttime = time(); // get the difference between last turn change and the current time $dif = $currenttime - $lastturnchange; // workout how many minutes until the next turn : part 1 $nextturnmin = $dif/60; // workout how many minutes until the next turn : part 2 $nextturn = 60 - $nextturnmin; // if the time until the next turn is less then zero, set the time till next turn to 0 if ($nextturnmin < 0) { $nextturnmin = 0; } // return the time until next turn change return $nextturnmin; } Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361552 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 try this <?php // Workout the time since the last turn change function lastturn($time) { $rs = mysql_connect( "localhost", "@@@@@@", "@@@@@" ); $rs = mysql_select_db( "@@@@" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); } // Workout the time until the next turn change function nextturn() { $conn = mysql_connect( "localhost", "@@@@@", "@@@@@" ); $sdb = mysql_select_db( "@@@@" ); // SQL query for all entries in descending order $sql = "SELECT `lastturn` FROM `cf_info`"; $SQLq = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $RS = mysql_fetch_assoc( $SQLq ); $lastturntime = $RS['lastturn']; // grab time since last turn change $lastturnchange = $lastturntime; // get the current time $currenttime = time(); // get the difference between last turn change and the current time $dif = $currenttime - $lastturnchange; // workout how many minutes until the next turn : part 1 $nextturnmin = $dif/60; // workout how many minutes until the next turn : part 2 $nextturn = 60 - $nextturnmin; // if the time until the next turn is less then zero, set the time till next turn to 0 if ($nextturnmin < 0) { $nextturnmin = 0; } // return the time until next turn change return $nextturnmin; } PS remove some details from your last post EDIT: reposted (posted old code) Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361556 Share on other sites More sharing options...
nikkieijpen Posted October 4, 2007 Share Posted October 4, 2007 $lastturnchange = $lastturntime[0]; Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361558 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 still nothing in the database and its showing: 19858238.0666 how can I make it display the time in just minutes? current code: <?php // Workout the time since the last turn change function lastturn($time){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); } // Workout the time until the next turn change function nextturn(){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "SELECT `lastturn` FROM `cf_info`"; $SQLq = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $rss = mysql_fetch_assoc( $SQLq ); $lastturntime = $rss['lastturn']; // grab time since last turn change $lastturnchange = $lastturntime[0]; // get the current time $currenttime = time(); // get the difference between last turn change and the current time $dif = $currenttime - $lastturnchange; // workout how many minutes until the next turn : part 1 $nextturnmin = $dif/60; // workout how many minutes until the next turn : part 2 $nextturn = 60 - $nextturnmin; // if the time until the next turn is less then zero, set the time till next turn to 0 if ($nextturnmin < 0) { $nextturnmin = 0; } // return the time until next turn change return $nextturnmin; } Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361589 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 Just wondering whats the field type in the database ? as if its timestamp your need to format the date/time (ie 2007-10-04 11:00:00) Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361594 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 int(150) Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361605 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 <?php // Workout the time since the last turn change function lastturn($time){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); } // Workout the time until the next turn change function nextturn(){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "SELECT `lastturn` FROM `cf_info`"; $SQLq = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); $rss = mysql_fetch_assoc( $SQLq ); $lastturntime = $rss['lastturn']; // grab time since last turn change $lastturnchange = $lastturntime; //[0]; // get the current time $currenttime = time(); // get the difference between last turn change and the current time $dif = $currenttime - $lastturnchange; // workout how many minutes until the next turn : part 1 $nextturnmin = ceil($dif/60); //round up (remove the seconds) // workout how many minutes until the next turn : part 2 $nextturn = 60 - $nextturnmin; // if the time until the next turn is less then zero, set the time till next turn to 0 if ($nextturnmin < 0) { $nextturnmin = 0; } // return the time until next turn change return $nextturnmin; } Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361623 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 now its: 19858291 still not in the database either. Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361627 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 can you function lastturn($time){ $rs = mysql_connect( "localhost", "ace_ACE", "*****" ); $rs = mysql_select_db( "ace_cf" ); // SQL query for all entries in descending order $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; echo $sql; //add this (see what we get) $rs = mysql_query( $sql ) or die('Query:<br />' . $sql . '<br /><br />Error:<br />' . mysql_error()); } Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361629 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 UPDATE `cf_info` SET lastturn = 1191502291 Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361665 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 can you run that in PhpMyadmin and see if it works! it looks ok to me Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361672 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 UPDATE `cf_info` SET lastturn = `1191502291`; that gets an error: Unknown Column '1191502291' in 'field list' Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361673 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 I have a cronjob that runs a PHP file every hour. I have made 2 functions, 1 to workout how long it has been since the last time the cornjob has run, and 1 to workout how long it is until the next cronjob. Wouldn't that be an hour since the last run, and an hour untill the next? Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361680 Share on other sites More sharing options...
MasterACE14 Posted October 4, 2007 Author Share Posted October 4, 2007 yes lol, I knew someone would say that , the point of it is, its for my text game, and players need to know when the next "turn change"(cronjob) will be. So they can workout the best time to attack their enemies etc. Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361683 Share on other sites More sharing options...
MadTechie Posted October 4, 2007 Share Posted October 4, 2007 UPDATE `cf_info` SET lastturn = `1191502291`; should be UPDATE `cf_info` SET lastturn = '1191502291'; or UPDATE `cf_info` SET lastturn = 1191502291; Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361695 Share on other sites More sharing options...
MasterACE14 Posted October 5, 2007 Author Share Posted October 5, 2007 I tried both of those ways, and the first one with the ' echo's ' . $time . ' so I removed the 2 periods and it echo'd the time correctly. I did the other way, that echo'd it fine as well, but neither inserted into the database Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-362107 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 I don't know why this isn't working? with my cron file. <?php // cron job include_once '/home/ace/public_html/conflictingforces/functions.php'; lastturn(time()); nextturn(); ?> putting the time() function into the lastturn() function is the correct function to use? Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363098 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 bump Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363389 Share on other sites More sharing options...
MasterACE14 Posted October 6, 2007 Author Share Posted October 6, 2007 <^>FaNcY BuMp<^> Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363412 Share on other sites More sharing options...
BlueSkyIS Posted October 6, 2007 Share Posted October 6, 2007 $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; Are there records in the table cf_info? Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363424 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 nope Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363642 Share on other sites More sharing options...
MasterACE14 Posted October 7, 2007 Author Share Posted October 7, 2007 %& Bump &% Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363791 Share on other sites More sharing options...
BlueSkyIS Posted October 7, 2007 Share Posted October 7, 2007 $sql = "UPDATE `cf_info` SET lastturn = " . $time . ""; Are there records in the table cf_info? If there are no records in the database, then there are no records to update, therefore "UPDATE" will do nothing. Quote Link to comment https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.