View Full Version : setting up a time schedule?
angst
09-28-2005, 08:04 PM
Hi,
I have a php script that needs to run 6 times a day, the documents that came with it suggest that it should be setup as a cron job,
but where my site is hosted, I'm unable to do this.
so I thought that maybe I could setup a php time schedule, so that the script will run inbetween these times when a users hits the site:
12am-1am
4am-5am
8am-9am
12pm-1pm
4pm-5pm
8pm-9pm
but also I'm wondering how the script could keep track if the script has already been ran for a particular time period.
any ideas how this could be done?
thanks in advance for your time!
-Ken
marek_mar
09-28-2005, 09:25 PM
You'd just have to search. There are some threads that have the anwsers.
Fou-Lu
09-29-2005, 05:11 AM
Hey ken,
Just make a text file, write a timestamp for it, import it into your current script. Check last run time and compare it to current time, execute if nessessary.
This can be done with mysql or other methods as well. Textfile I figure will be the quickest and easiest to set up though.
angst
09-29-2005, 02:45 PM
yah i was thinking a text file would be best too,
so thats what i started on, but i'm having trouble comparing the two values,
my code:
<?
$file = file('time.log');
$LastLineTmp = end($file);
echo $LastLineTmp;
echo "<br />";
$hour = date(G);
echo $hour;
if ($LastLineTmp == $hour ) {
echo "Log is up to date";
} else {
//write to log
$textline = $hour;
$filename = "time.log";
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit; }
$textline.="\n";
now write content to our opened file.
IF (fwrite($handle,$textline) === FALSE)
{echo "Cannot write to file ($filename)";
exit;}
echo "Success!";
fclose($handle);
//
}
?>
this is the part thats not working:
if ($LastLineTmp == $hour ) {
basicly, if the lastline of the log = the same as the hour, then it doesn't need to running again, but it does,
any idea whats going on here? the $LastLineTmp seems to be the one causing the problem.
thanks again for your time!
-Ken
Fou-Lu
09-29-2005, 04:35 PM
I remember that one of the filereading functions returns its values with a final whitespace. Unfortunatly, I cannot remember if its file(). Try trimming the value of $lastlinetmp with either trim() or rtrim() and see if that solves it up.
Also, according to your php posted, this: now write content to our opened file. is defined as a variable. Could just be your comments as well, vB I find to be kind of difficult while dealing with them at times.
As well, I'd recommend a full timestamp on the inserted information and compare with it from there. You can of course use the date() function, just use the timestamp as the second parameter. I say this as if the day is different but the hour is the same, the script will assume that all is currently well, so I'd recommend passing the value so you can compare the date as well.
angst
09-29-2005, 06:37 PM
ok, i think i've got it,,
how does this look:
//check and write to file//
Function CheckWriteLog($tvalue)
{
$filename = 'time.log';
$file = file($filename);
$LastLineTmp = end($file);
$hour = $tvalue;
$LastLineTmp = substr($LastLineTmp, 0, 2);
if ($LastLineTmp == $hour ) {
If ($LastLineTmp == 20 ) {
//clear file function//
$somecontent = "";
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
//end clear file//
}
//do nothing
} else {
//write to log
$textline = $hour;
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit; }
$textline.="\n";
//now write content to our opened file.
IF (fwrite($handle,$textline) === FALSE)
{echo "Cannot write to file ($filename)";
exit;}
//echo "Success!";
fclose($handle);
}
}
$hourtmp = date("H");
IF ($hourtmp == 00 or $hourtmp > 00 && $hourtmp < 01) {
CheckWriteLog(00);
} elseIF ($hourtmp == 04 or $hourtmp > 04 && $hourtmp < 05) {
CheckWriteLog(04);
} elseIF ($hourtmp == 08 or $hourtmp > 08 && $hourtmp < 09) {
CheckWriteLog(08);
} elseIF ($hourtmp == 12 or $hourtmp > 12 && $hourtmp < 13) {
CheckWriteLog(12);
} elseIF ($hourtmp == 16 or $hourtmp > 16 && $hourtmp < 17) {
CheckWriteLog(16);
} elseIF ($hourtmp == 20 or $hourtmp > 20 && $hourtmp < 21) {
CheckWriteLog(20);
} else {
echo "not yet time for update";
}
I'm only writing the hour because this needs to be updated every 4 hours of the day, and then clears the log file at the begining of each day. I want to use this to update currencies for many countries around the world, they change so often.
-Ken
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.