...

View Full Version : Need Help with timing issues



matafy
10-23-2009, 03:02 AM
:confused: 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
$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";
}
?>

Fou-Lu
10-23-2009, 03:14 AM
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).


BTW, I figure that this octal is the primary problem with this 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).

matafy
10-23-2009, 03:32 AM
Would it look more like this?


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

And I would call like this?

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

Fou-Lu
10-23-2009, 03:39 AM
Close, its the comparison seems to be off:


if ($hour < '09' || $hour > '21')
{

or more accurately (if you're like me you prefer the strcmp route):


if (strcmp($hour, '09') < 0 || strcmp($hour, '21') > 0)
{

Yeah, that looks like it should work.

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.

matafy
10-23-2009, 03:49 AM
This is what It looks like, if I understand it correctly.


<?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);?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum