Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k 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 Reply

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...