繁体   English   中英

PHP 时区和夏令时问题

[英]PHP timeZone and Daylight savings issue

我的代码有问题。 我必须将所有日历午餐时间存储为 UTC 时间。 现在转换为我的时区工作正常。 但是,当有人在夏令时更改后展望未来时,午餐时间不正确并偏移一个小时。

    $startTime = strtotime($start_date . " UTC");
    $endTime = strtotime($end_date . " UTC");
    
    date_default_timezone_set($timeZone);
    
    $calendarWorkHourExceptions->StartDate = date("Y-m-d H:i:s", $startTime);
    $calendarWorkHourExceptions->EndDate = date("Y-m-d H:i:s", $endTime); 

我还有用户在日历中查看的日期范围。 如果有人能如此好心地帮助我,我将不胜感激。

一般来说,我会尝试使用 PHP 的 DateTime 和 DateTimeZone 类,因为它可以更轻松地处理时区:

# define both time zone objects
$utcTimeZoneObj   = new DateTimeZone('UTC');
$localTimezoneObj = new DateTimeZone( $timeZone );

# define start and end time objects in UTC
$startDateTimeObj = new DateTime( $start_date, $utcTimeZoneObj );
$endDateTimeObj   = new DateTime( $end_date, $utcTimeZoneObj );

# change the time zone of the start and get the formatted date
$startDateTimeObj->setTimezone( $localTimezoneObj );
$calendarWorkHourExceptions->StartDate = $startDateTimeObj->format( 'Y-m-d H:i:s' );

# change the time zone of the end and get the formatted date
$endDateTimeObj->setTimezone( $localTimezoneObj );
$calendarWorkHourExceptions->EndDate = $endDateTimeObj->format( 'Y-m-d H:i:s' );

但是,在处理日历时,您应该以当地时间存储时间。 我遇到了同样的问题。 有关更多信息,请参阅: java 多时区应用程序的日历、日期和时间管理

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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