0

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.

Pieter de Vries
  • 101
  • 2
  • 13

1 Answers1

0

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

Alan
  • 702
  • 6
  • 14