...

View Full Version : How to run this php file every hour ??



oneofthelion
10-28-2009, 01:13 AM
Thanks but how do I run it for the first time?

<?php //This is my code I want to run every hour.

$lastRun = time();
if ($lastRun + 3600 < time())
{
runIt();

$json = file_get_contents('http://some_api');
$data = json_decode($json);
$ttopics = array();

foreach ($data->trends as $trend)
{
$tt[] = $trend->name ;
}

foreach ($tt as $ttkey)
{
echo $ttkey.'<br \>';
}
}

?>

I know I am making some blunder.. please correct me.

PappaJohn
10-28-2009, 01:32 AM
You will need to run the script as a Cron Job (assuming *nix server).

mlseim
10-28-2009, 01:41 AM
With a CRON Job ...

But if it's on a web page that is accessed by users at least
once per hour, you could simply count on the visitor to execute
it when the page is viewed (the PHP script gets executed).

To run it from "users", you would need to use a simple text file
to save the UNIX timestamp when it is run, and then compare
it each time to the current time ... if the integers are apart by
3600, you know it's been at least an hour (3600 seconds = 60 minutes).

Make a file called "time.db" and CHMOD it as 0777


<?php
// name of your file
$myFile="time.db";
$time=file($myFile);
if(time()-3600 > $time[0]){
// an hour has elapsed
// do your thing.

// write the new timestamp to file
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, time());
fclose($fh);
}
else{
// it hasn't been an hour yet, so do nothing
}
?>

oneofthelion
10-31-2009, 07:50 AM
With a CRON Job ...

My godaddy allows cron at particular time...but I need to run the script as per time logged(last run)



But if it's on a web page that is accessed by users..

No it is a script which runs without user interface



you would need to use a simple text file
to save the UNIX timestamp when it is run...



Make a file called "time.db" and CHMOD it as 0777

Is the below php file the same time.db?? I dont think Godaddy allows me to CHMOD any file...does it?



<?php
// name of your file
$myFile="time.db";
$time=file($myFile);
if(time()-3600 > $time[0]){
// an hour has elapsed
// do your thing.

// write the new timestamp to file
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, time());
fclose($fh);
}
else{
// it hasn't been an hour yet, so do nothing
}
?>

oneofthelion
10-31-2009, 10:58 AM
My script obviously did not run the next time...PLEASE CORRECT ME



require_once("includes/connection.php");

$query = mysql_query("SELECT lastRun FROM time") or die(mysql_error());
if(mysql_num_rows($query)!=0)
{
while($row = mysql_fetch_array($query))
{
$TimeRun = $row['lastRun'];
if ($TimeRun + 3600 < time())
{
$to_email = "salman_ahad@yahoo.com";
$subject = "test script";
$message =
"Time : Running";

mail($to_email, $subject, $message);
$TimeRun = time();
$querypost = mysql_query ("UPDATE time SET lastRun = '$TimeRun'");
}
}
}
else{
//nothing
}


OR..could I do?



function first(){
//All my code

sleep(3600);
call function second()
}

function second(){
//All my same code as above

sleep(3600);
call function first()
}

mlseim
10-31-2009, 06:19 PM
Your MySQL method looks OK ... not sure why it would not work.
You're sure that the timestamp is getting written into the database properly?
It's an integer that's 10 characters long.

Your CRON needs to check it every 1 minute, since you don't know when it
first ran within the hour.

Put an echo in so you can make sure it is grabbing the correct variable ...

while($row = mysql_fetch_array($query))
{
$TimeRun = $row['lastRun'];
echo $TimeRun;
exit;

That will test it by displaying the value and stopping the script.

oneofthelion
11-01-2009, 08:53 AM
@mlseim : Thanks for trying and helping me here.

Lets go over my requirement once more so you can help me better.



//test.php

//"users" table contains multiple users

//"message" table contains multiple messages

$user = mysql_query("SELECT * FROM users") or die(mysql_error());
$message = mysql_query("SELECT * FROM message") or die(mysql_error());

//...guide me for rest of the code

//Every user should be sent a message every one hour. Also those messages which were not sent to any user before.

//Lets say
user-1 was sent a message-23 at 01:02:03 // user-1 shld receive the next message in queue after one hour
user-2 was sent a message-40 at 20:22:00 // user-2 shld receive the next message in queue after one hour
...
user-n was sent a message-78 at 12:30:10 // user-n shld receive the next message in queue after one hour



So basically we need a script which is activated whenever it is time for any user is required to be sent a message...it should come to this test.php class and check which message is available..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum