Jim
Jim

Reputation: 3

Weird behavior calculating timezone difference

I can't explain this. I have the following:

     $time += $res['timezone']; (The array equates to -5*3600 (EST))
     return gmstrftime('%c',$time);

When I echo $res['timezone'], I get "-5*3600" which is correct. When I put the array value in front of the time variable, I get the incorrect time. If I comment out the array value and replace it with -5*3600, I get the correct result. Why??

Upvotes: 0

Views: 107

Answers (1)

Marius
Marius

Reputation: 58911

because the string "-5*3600" and the expression -5*3600 aren't the same thing. You could try to put eval around the array value, like so:

 $time += eval($res['timezone']); //(The array equates to -5*3600 (EST))
 return gmstrftime('%c',$time);

Note that this is a very bad idea, as it is both slow and insecure. If you want to store -5*3600 in the array, then calculate the value and store the result in the array:

$res['timezone'] = -5*3600;

Upvotes: 1

Related Questions