zomega
zomega

Reputation: 2316

PHP daylight saving wrong timezone

This script should print

3600
7200

but it prints

3600
3600

Why? As far as I know php supports daylight saving.

date_default_timezone_set("Europe/Berlin");

$date1 = new DateTime();
$date1->setDate(2019,1,1); // winter time
$date1->setTime(0,0,0,0);

$date2 = new DateTime();
$date2->setDate(2019,6,1); // summer time
$date2->setTime(0,0,0,0);

$ref = new DateTime("now", new DateTimeZone("UTC"));
echo $date1->getTimeZone()->getOffset($ref);
echo "\n";
echo $date2->getTimeZone()->getOffset($ref);
echo "\n";

Upvotes: 1

Views: 222

Answers (1)

Matt Johnson-Pint
Matt Johnson-Pint

Reputation: 241505

Your $ref variable is representing "now". In the echo statements, you pass $ref to getOffset, so it is getting the current offset as of now. Instead, pass $date1 or $date2.

echo $date1->getTimeZone()->getOffset($date1);
echo "\n";
echo $date2->getTimeZone()->getOffset($date2);
echo "\n";

Or better yet, just call getOffset() directly:

echo $date1->getOffset();
echo "\n";
echo $date2->getOffset();
echo "\n";

Upvotes: 4

Related Questions