Jump to content

cronjob and timer


MasterACE14

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/
Share on other sites

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;
}

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361552
Share on other sites

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)

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361556
Share on other sites

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;
}

 

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361589
Share on other sites

<?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;
}

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361623
Share on other sites

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());
}

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361629
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-361680
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/71794-cronjob-and-timer/#findComment-363098
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.