Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-945842
Share on other sites

Need more clarification on the solution someone gave me

 

I need to run apart from cron, it is a script...

 

Someone gave me this solution..

 

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

PHP Code:

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

}

?>

 

I am not sure if Godaddy allows me chmod any file... need more clarification.

 

 

Link to comment
https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948224
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948246
Share on other sites

.....cron job on your godaddy hosting

 

any cron job allows us to run a php file at specific time...but this is what I am trying to do

 

//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 the message which was not sent to any user.

//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

 

Can this user specific script be run as cron??

Link to comment
https://forums.phpfreaks.com/topic/179265-apart-from-cron/#findComment-948451
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.