EnergyLynx EnergyLynx
EnergyLynx EnergyLynx

Reputation: 115

PHP strtotime is wrong (some times)?

I have this issue with strtotime in my PHP code that it is wrong sometimes (only for some timezones) and it is correct for others!!

I cannot get my head around it.

I have set the <?php date_default_timezone_set('GMT'); ?> at the top of my page as well but that doesn't help!

basically what it does is that it will add or subtract the offset/3600 value to the set time in $time1 = strtotime('00:00'); depending on the if and else if conditions.

the offset/3660 value is the time difference between two timezones!

the code bellow works for some locations and it doesn't for others! basically it will add an extra 1-2 hours or takes off/subtract an extra 1-2 hours some times (not all the time).

i.e. the time difference between Abidjan and london is -1. the time (value) that should be shown is 23:00 as 00:00 - 01:00 = 23:00. but the value is shown is 00:00.

However as i mentioned it works for some timezones. i.e. for the time difference between New York and London which is -5 the code works and it shows 19:00 as 00:00 - 05:00 = 19:00

could someone please shed a light on this?

here is the code in question:

<?php
$time1 = strtotime('00:00');

if (0 > $offset)
{
   // For negative offset (hours behind)
  $hour_dif = date('H:i', strtotime($time1 -$offset/3600));
  $time1 = "{$hour_dif}";
}
elseif (0 < $offset)
{
   // For positive offset (hours ahead)
     $hour_dif = date('H:i', strtotime($time1 +$offset/3600));
     $time1 = "{$hour_dif}";

}
else
{
   // For offsets in the same timezone.
   $time1 = "in the same timezone";
}

echo "{$time1}";
?>

Upvotes: 1

Views: 656

Answers (1)

Cobra_Fast
Cobra_Fast

Reputation: 16061

Well, since strtotime() already returns a timestamp and date() expects one you could just do

$hour_dif = date('H:i', ($time1 - ($offset*3600)));

or

$hour_dif = date('H:i', ($time1 + ($offset*3600)));

respectively, to remove or add the correct amount of seconds from/to the timestamp.

I am also assuming that $offset is the offset in hours, so you would have to multiply by 3600 to get the amount of seconds, not divide.


Alright, after testing your code and thinking through it some more, it became obvious.

With a negative offset like -1 you're going to calculate $time1 - (-1) * 3600 and we all know that a double negation is positive...

So in fact, your code can be condensed to:

$time1 = strtotime('00:00');

if ($offset == 0)
     $time1 = "in the same timezone";
else
{
   // For positive offset (hours ahead)
     $hour_dif = date('H:i', ($time1 + ($offset*3600)));
     $time1 = "{$hour_dif}";
}

echo "{$time1}\n";

and should work as expected:

cobra@box ~ $ for i in {-24..24}; do php test.php $i; done;
00:00
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
in the same timezone
01:00
02:00
03:00
04:00
05:00
06:00
07:00
08:00
09:00
10:00
11:00
12:00
13:00
14:00
15:00
16:00
17:00
18:00
19:00
20:00
21:00
22:00
23:00
00:00

Upvotes: 3

Related Questions