Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: PHP time

  1. #11
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Twey: Thanks for the tip I've never heard that before. Also I might have done my math wrong, but won't your code only run update() once a day. I think it should be this.
    PHP Code:
    <?php
    define
    ('DATA_FILE''update_tracker.dat');
    $c floor(date('U') / 60 60);
    //Removed the extra /24 above
    $fc file(DATA_FILE) or die('Failed to open file for reading.');
    $l $fc[0];

    while(
    $l++ < $c)
      
    update();

    $f fopen(DATA_FILE'w') or die('Failed to open file for writing.');
    fwrite($f$c);
    fclose($f);
    ?>

  2. #12
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I understand the functions going on in that script, but can one of you please explain to me the theory behind the script? Like, the while loop in particular.

  3. #13
    Join Date
    Sep 2005
    Posts
    882
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    The file update_tracker.dat is used to keep track of when update() was last run.When the script loads it compares the current time($c) to the time held in update_tracker.dat($l).The while loop is used to run update() once for every hour that has passed.Then the current time is saved to update_tracker.dat

  4. #14
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Also I might have done my math wrong, but won't your code only run update() once a day.
    Yep, you're right. I was never good at maths.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  5. #15
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Dang this is confusing.
    I get everything except, lets say someone accesses the page and that script is run, then 2 hours later the script is run again, that would write to the file something like 2:00pm, 4:00pm, but it would leave out 3:00pm so wouldn't the update not happen at 3?

  6. #16
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's what my code is for.

    Breaking them both down into English, on each access, blm126's code will
    • open the file and check if it's been run this hour;
    • if so, run the function;
    • update the file.
    Mine will
    • open the file and check when it was last run;
    • for each hour in which it wasn't run, call the function once;
    • update the file.
    The updates won't necessarily happen on time, but they will all happen.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #17
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ooooohhhh.
    Okay, now i get it. I'm not good with date functions nor file functions.
    Thanks.

Bookmarks

Posting Permissions

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