简体   繁体   中英

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";

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM