Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-23-2009, 03:02 AM   PM User | #1
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
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";
    }
?>
matafy is offline   Reply With Quote
Old 10-23-2009, 03:14 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php

Last edited by Fou-Lu; 10-23-2009 at 03:20 AM..
Fou-Lu is offline   Reply With Quote
Old 10-23-2009, 03:32 AM   PM User | #3
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
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);?>
matafy is offline   Reply With Quote
Old 10-23-2009, 03:39 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Old 10-23-2009, 03:49 AM   PM User | #5
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
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);?>
matafy is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:18 PM.


Advertisement
Log in to turn off these ads.