public static function shiftToTimezone(BusinessHours $businessHours, \DateTimeZone $newTimezone): BusinessHours
    {
        $now = new \DateTime('now');
        $oldTimezone = $businessHours->getTimezone();
        $offset = $newTimezone->getOffset($now) - $oldTimezone->getOffset($now);

        if ($offset === 0) {
            return clone $businessHours;
        }

        $tmpDays = \array_fill_keys(Day::getDaysOfWeek(), []);
        foreach ($businessHours->getDays() as $day) {
            foreach ($day->getOpeningHoursIntervals() as $interval) {
                $start = $interval->getStart()->toSeconds() + $offset;
                $end = $interval->getEnd()->toSeconds() + $offset;

                // Current day.
                if ($start < 86400 && $end > 0) {
                    $startForCurrentDay = \max($start, 0);
                    $endForCurrentDay = \min($end, 86400);

                    $dayOfWeek = $day->getDayOfWeek();
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForCurrentDay),
                        TimeBuilder::fromSeconds($endForCurrentDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }

                // Previous day.
                if ($start < 0) {
                    $startForPreviousDay = 86400 + $start;
                    $endForPreviousDay = \min(86400, 86400 + $end);

                    $dayOfWeek = self::getPreviousDayOfWeek($day->getDayOfWeek());
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForPreviousDay),
                        TimeBuilder::fromSeconds($endForPreviousDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }

                // Next day.
                if ($end > 86400) {
                    $startForNextDay = \max(0, $start - 86400);
                    $endForNextDay = $end - 86400;

                    $dayOfWeek = self::getNextDayOfWeek($day->getDayOfWeek());
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForNextDay),
                        TimeBuilder::fromSeconds($endForNextDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }
            }
        }

        $tmpDays = \array_filter($tmpDays);
        $days = self::flattenDaysIntervals($tmpDays);

        return new BusinessHours($days, $newTimezone);
    }
public static function shiftToTimezone(BusinessHours $businessHours, \DateTimeZone $newTimezone): BusinessHours
    {
        $now = new \DateTime('now');
        $oldTimezone = $businessHours->getTimezone();
        $offset = $newTimezone->getOffset($now) - $oldTimezone->getOffset($now);

        if ($offset === 0) {
            return clone $businessHours;
        }

        $tmpDays = \array_fill_keys(Day::getDaysOfWeek(), []);
        foreach ($businessHours->getDays() as $day) {
            foreach ($day->getOpeningHoursIntervals() as $interval) {
                $start = $interval->getStart()->toSeconds() + $offset;
                $end = $interval->getEnd()->toSeconds() + $offset;

                // Current day.
                if ($start < 86400 && $end > 0) {
                    $startForCurrentDay = \max($start, 0);
                    $endForCurrentDay = \min($end, 86400);

                    $dayOfWeek = $day->getDayOfWeek();
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForCurrentDay),
                        TimeBuilder::fromSeconds($endForCurrentDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }

                // Previous day.
                if ($start < 0) {
                    $startForPreviousDay = 86400 + $start;
                    $endForPreviousDay = \min(86400, 86400 + $end);

                    $dayOfWeek = self::getPreviousDayOfWeek($day->getDayOfWeek());
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForPreviousDay),
                        TimeBuilder::fromSeconds($endForPreviousDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }

                // Next day.
                if ($end > 86400) {
                    $startForNextDay = \max(0, $start - 86400);
                    $endForNextDay = $end - 86400;

                    $dayOfWeek = self::getNextDayOfWeek($day->getDayOfWeek());
                    $interval = new TimeInterval(
                        TimeBuilder::fromSeconds($startForNextDay),
                        TimeBuilder::fromSeconds($endForNextDay)
                    );
                    $tmpDays[$dayOfWeek][] = $interval;
                }
            }
        }

        $tmpDays = \array_filter($tmpDays);
        $days = self::flattenDaysIntervals($tmpDays);

        return new BusinessHours($days, $newTimezone);
    }
public static function shiftToTimezone ( BusinessHours $ businessHours , \ DateTimeZone $ newTimezone ) : BusinessHours { $ now = new \ DateTime ( 'now' ) ; $ oldTimezone = $ businessHours -> getTimezone ( ) ; $ offset = $ newTimezone -> getOffset ( $ now ) - $ oldTimezone -> getOffset ( $ now ) ; if ( $ offset === 0 ) { return clone $ businessHours ; } $ tmpDays = \ array_fill_keys ( Day :: getDaysOfWeek ( ) , [ ] ) ; foreach ( $ businessHours -> getDays ( ) as $ day ) { foreach ( $ day -> getOpeningHoursIntervals ( ) as $ interval ) { $ start = $ interval -> getStart ( ) -> toSeconds ( ) + $ offset ; $ end = $ interval -> getEnd ( ) -> toSeconds ( ) + $ offset ; // Current day. if ( $ start < 86400 && $ end > 0 ) { $ startForCurrentDay = \ max ( $ start , 0 ) ; $ endForCurrentDay = \ min ( $ end , 86400 ) ; $ dayOfWeek = $ day -> getDayOfWeek ( ) ; $ interval = new TimeInterval ( TimeBuilder :: fromSeconds ( $ startForCurrentDay ) , TimeBuilder :: fromSeconds ( $ endForCurrentDay ) ) ; $ tmpDays [ $ dayOfWeek ] [ ] = $ interval ; } // Previous day. if ( $ start < 0 ) { $ startForPreviousDay = 86400 + $ start ; $ endForPreviousDay = \ min ( 86400 , 86400 + $ end ) ; $ dayOfWeek = self :: getPreviousDayOfWeek ( $ day -> getDayOfWeek ( ) ) ; $ interval = new TimeInterval ( TimeBuilder :: fromSeconds ( $ startForPreviousDay ) , TimeBuilder :: fromSeconds ( $ endForPreviousDay ) ) ; $ tmpDays [ $ dayOfWeek ] [ ] = $ interval ; } // Next day. if ( $ end > 86400 ) { $ startForNextDay = \ max ( 0 , $ start - 86400 ) ; $ endForNextDay = $ end - 86400 ; $ dayOfWeek = self :: getNextDayOfWeek ( $ day -> getDayOfWeek ( ) ) ; $ interval = new TimeInterval ( TimeBuilder :: fromSeconds ( $ startForNextDay ) , TimeBuilder :: fromSeconds ( $ endForNextDay ) ) ; $ tmpDays [ $ dayOfWeek ] [ ] = $ interval ; } } } $ tmpDays = \ array_filter ( $ tmpDays ) ; $ days = self :: flattenDaysIntervals ( $ tmpDays ) ; return new BusinessHours ( $ days , $ newTimezone ) ; }
Create a new BusinessHours with a different timezone from an existing BusinessHours.

@param BusinessHours $businessHours The original business hours.
@param \DateTimeZone $newTimezone The new timezone.
@return BusinessHours
Create a new BusinessHours with a different timezone from an existing BusinessHours .