簡體   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