Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.5k views
in Technique[技术] by (71.8m points)

javascript - PHP timeZone and Daylight savings issue

I have an issue with my code. I have to store all my calendar lunch hours as UTC time. Now converting to my timezone works fine. However when someone looks in the future after daylight saving changes the lunch hours are incorrect and offset by an hour.

    $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); 

I also have the date range the user is looking at in the calendar. If someone would be so kind in helping me I would really appreciate that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In general, I would try using PHP's DateTime and DateTimeZone classes as it makes dealing with time zones easier:

# 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' );

HOWEVER, when dealing with calendars, you should store the times in local time. I ran into the same issue. See this for more info: java Calendar, Date, and Time management for a multi-timezone application


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...