Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to run this php file every hour ??

    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.

  • #2
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    You will need to run the script as a Cron Job (assuming *nix server).
    John

  • #3
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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 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($fhtime());
    fclose($fh);
    }
    else{
    // it hasn't been an hour yet, so do nothing
    }
    ?>

  • #4
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    With a CRON Job ...
    My godaddy allows cron at particular time...but I need to run the script as per time logged(last run)

    Quote Originally Posted by mlseim View Post
    But if it's on a web page that is accessed by users..
    No it is a script which runs without user interface

    Quote Originally Posted by mlseim View Post
    you would need to use a simple text file
    to save the UNIX timestamp when it is run...
    Quote Originally Posted by mlseim View Post
    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 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($fhtime());
    fclose($fh);
    }
    else{
    // it hasn't been an hour yet, so do nothing
    }
    ?>

  • #5
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    My script obviously did not run the next time...PLEASE CORRECT ME

    PHP Code:
    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?

    PHP Code:
    function first(){
    //All my code

    sleep(3600);
    call function second()
    }

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

    sleep(3600);
    call function first()


  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,519
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    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.

  • #7
    New Coder
    Join Date
    Oct 2009
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    @mlseim : Thanks for trying and helping me here.

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

    PHP Code:
    //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..


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •