Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: PHP time

  1. #1
    Join Date
    Apr 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP time

    How could i display diffrent lines of text based on the time, for example i want to make it so when my server time is 2:00am it will display that we are open untill the server time is 3:00am. Then it will go back to saying that we are closed, then again at 9:00pm untill 10:00pm it will say that we are open.

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

    Default

    Code:
    <?php $hr = date('G'); ?>
    <?php if($hr == 2 || $hr == 9) { ?>
    
    <p>We're open.</p>
    
    <?php } else { ?>
    
    <p>We're closed.</p>
    
    <?php } ?>
    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!

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

    Default

    Is there anyway to do something by the hour? Like every hour function update() is called, and it updates every users information.

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

    Default

    Cron Jobs are probably better at this then PHP.However yo can do it with PHP like this.
    PHP Code:
    <?php
    function update(){
    //this will be called once per hour
    return true;
    }
    $current_time mktime();
    $last_updated file_get_contents("last_updated.txt");
    $diff $current_time $last_updated;
    if(
    $diff >= 3600){
    //it has been an hour since update
        
    update();
        if(
    $fp = @fopen("last_updated.txt","w")) { 
            
    //Write time to file
              
    $contents fwrite($fp,$current_time); 
              
    fclose($fp);  
        }
        else{
        die(
    "Could not write to file")
        }
    }
    ?>

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

    Default

    Aha, brilliant. Thank you.

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

    Default

    Hmm... but if nobody visits it for an hour, that hour's update will be neglected. I'd keep current. This can also be simplified drastically:
    Code:
    define('DATA_FILE', 'update_tracker.dat');
    $c = floor(date('U') / 60 / 60 / 24);
    $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);
    blm126: Single quotes are significantly faster to parse than double, and so should be used wherever double quotes are not needed.
    Last edited by Twey; 07-24-2006 at 10:00 PM.
    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. #7
    Join Date
    Feb 2006
    Posts
    158
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Twey, would you mind explaining your code please? It's pretty complicated to me, i dont know how to work with files yet.

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

    Default

    I've linked to the relevant documentation. Does that help?
    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!

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

    Default

    Great. Thanks.

  10. #10
    Join Date
    Apr 2006
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey
    Code:
    <?php $hr = date('G'); ?>
    <?php if($hr == 2 || $hr == 9) { ?>
    
    <p>We're open.</p>
    
    <?php } else { ?>
    
    <p>We're closed.</p>
    
    <?php } ?>
    it works, thank you!

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
  •