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 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2009
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Need Help with timing issues

    I don't know what is going wrong. I need some help with being able to set an image at 9:00am Colorado time if my server is in California and it would be 8:00am.

    PHP Code:
    <?php
    $hours_offset 
    1// Server time offset
    function currentImage(){ 
        global 
    $hours_offset;
        
    $hour date("H",(time()-(3600*$hours_offset)));
        
    $imagePath "img/"
        if (
    date(H) < 09 || date(H) > 21) { 
        return 
    $imagePath."".date(D)."/".$hour.".png";
        }
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Is you're timezone properly set to california time? That would be the first step, either set that at california time for local, or set it to GMT to use as you're base time.
    Next, the problem is likely due to this: date(H) < 09. First and foremost, technically the date wants a string, not a constant. Fortunately, this will just trigger notices in PHP; it will interpret non-defined constants as strings, so it still sees it as date('H'). Best to change them to strings though. The problem with the code is the 09. That is an incorrect octal number, which will be interpreted as 0 in an integer comparison. What you want is '9' or 9 (the string is more accurate since date() returns a string).

    And a couple of extra notes. The first is that you can simplify this with strtotime. strtotime('+1 hour'); when time is 8pm will return 9pm. Next, this is bad: global $hours_offset;. Never globalize any variable in a function or method where you can redefine the signature. The only situations where you should need to globalize are where you require particular callback signatures, functions like usort, set_error_handler (this you'll find is often globalizing a file handle), etc. Instead, alter you're signature to take a parameter for it: function currentImage($hours_offset) and call it with the argument. Doing this eliminates the need to understand the code with extensions (and in particular, comparing to a language like C where its compiled, you cannot view what the name of the variable is, but it must be the same in order to be globalized).

    Edit:
    BTW, I figure that this octal is the primary problem with this code:
    Code:
    >php -r "print strcmp(date('H', strtotime('09:18')), 09);"
    >1
    >
    >
    >php -r "print strcmp(date('H', strtotime('09:18')), '09');"
    >0
    As you can see, the 09 is treated as an integer(0), so you'll want that to actually be a string (since date('H') returns zero padded if less than 12).
    Last edited by Fou-Lu; 10-23-2009 at 03:20 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Apr 2009
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts
    Would it look more like this?

    PHP Code:
     <?php
    $hours_offset 
    = -1// Server time offset
    function currentImage($hours_offset){ 
        
    $hour date("H",(time()-(3600*$hours_offset)));
        
    $imagePath "img/"
        if (
    date(H) < || date(H) > 21) { 
        return 
    $imagePath."".date(D)."/".$hour.".png";
        }
    ?>
    And I would call like this?
    PHP Code:
    <?php echo currentImage($hours_offset);?>

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Close, its the comparison seems to be off:
    PHP Code:
    if ($hour '09' || $hour '21')

    or more accurately (if you're like me you prefer the strcmp route):
    PHP Code:
    if (strcmp($hour'09') < || strcmp($hour'21') > 0)

    Yeah, that looks like it should work.
    Edit:
    Oh yes, it appears I missed mentioning this the first time around. You want to use $hour as you're comparison, not date('H'). date('H') works from the time() not the time() - offset you want.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    New Coder
    Join Date
    Apr 2009
    Posts
    59
    Thanks
    13
    Thanked 0 Times in 0 Posts
    This is what It looks like, if I understand it correctly.

    PHP Code:
    <?php
    $hours_offset 
    = -1// Server time offset
    function currentImage($hours_offset){ 
        
    $hour date("H",(time()-(3600*$hours_offset)));
        
    $imagePath "img/"
        if (
    $hour '09' || $hour '21') { 
        return 
    $imagePath."".date(D)."/".$hour.".png";
        }
    ?>

    <?php echo currentImage($hours_offset);?>


  •  

    Posting Permissions

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