Jump to content

cronjob and timer


MasterACE14

Recommended Posts

yes lol, I knew someone would say that  :D , 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.

 

Perhaps I'm missing the whole point here, but if your cron job runs every hour and the present time is 34 minutes past the hour isn't the next cron job in 26 minutes?  Why is this problem more complex than that?

Link to comment
Share on other sites

if its stored as an integer - then you need to pull it from the database as an integer, and print it out in date format.

 

so if you pulled it from the database and stored it in the variable $lasttime:

<?php
$lastrun = date("H:i", $lasttime); // this will output the last time the cron was run as 06:24 instead of an integer
//now you want to know how much time until the next cron is run
$nextrun = mktime(date("H",$lasttime)+1); //this adds 1 hour to the last time the cron was run, outputting 07:24
?>

Link to comment
Share on other sites

point taken, is it possible to do this, just with the current time? workout how long until the next hour, and workout how long it has been since the last one? That sounds like a better idea, then what I've currently been doing.

 

here's what I've done to get the current time in my timezone:

<?php
$date=date(U); // format date for calculation in seconds
//print ("$date");  //if you want to see the date format now
$hourdiff = "16"; //change the server time plus sixteen hours
$timeadjust = ($hourdiff * 60 * 60); //this is how to calc it
$adjtime = date("M j, H:i", $date + $timeadjust); //format display
print $adjtime;  //print the adjusted time

 

now if I could workout the time differences just by adding onto this script, I think that would be great.

Link to comment
Share on other sites

<?php
$lasttime = '1191759840';
$lastrun = date("H:i", $lasttime); // this will output the last time the cron was run as 06:24 instead of an integer
//now you want to know how much time until the next cron is run
$nextrun = date($lasttime)+3600;
$nextruntime = date("H:i", $nextrun); //this adds 1 hour to the last time the cron was run, outputting 07:24
$countdown =  $nextrun - time();
$countdownmins = round($countdown/60, 1);
echo "last turn was at $lastrun, the next run will be at $nextruntime, the time remaining until next run is in $countdownmins Minutes";
?>

 

Try something like this.

Link to comment
Share on other sites

That seems to be working great  ;D , I'm going to let it run through for about the next 8 hours to make sure the cronjob is running on time and that the timer script isn't stuffing up  :)

 

EDIT: I noticed that working out the next run and last run, it is displaying the time at the location of my Web Host, how could I change it to my time, which is what my date script above does. ?

 

thanks for your help yzerman and everyone else who helped me with this matter.

 

Regards ACE

Link to comment
Share on other sites

I'm starting to get some strange results, The timer works fine for a few turns, say 4 turns, and then after the 4th turn it says something like 120 minutes until the next one. I have to manually run the cron file then to make it go back to "60 minutes until next turn".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.