Gdn_Format::toTimezone PHP Method

toTimezone() public static method

Formats a timestamp to the current user's timezone.
public static toTimezone ( integer $Timestamp ) : integer
$Timestamp integer The timestamp in gmt.
return integer The timestamp according to the user's timezone.
    public static function toTimezone($Timestamp)
    {
        static $GuestHourOffset;
        $Now = time();
        // Alter the timestamp based on the user's hour offset
        $Session = Gdn::session();
        $HourOffset = 0;
        if ($Session->UserID > 0) {
            $HourOffset = $Session->User->HourOffset;
        } elseif (class_exists('DateTimeZone')) {
            if (!isset($GuestHourOffset)) {
                $GuestTimeZone = c('Garden.GuestTimeZone');
                if ($GuestTimeZone) {
                    try {
                        $TimeZone = new DateTimeZone($GuestTimeZone);
                        $Offset = $TimeZone->getOffset(new DateTime('now', new DateTimeZone('UTC')));
                        $GuestHourOffset = floor($Offset / 3600);
                    } catch (Exception $Ex) {
                        $GuestHourOffset = 0;
                        logException($Ex);
                    }
                }
            }
            $HourOffset = $GuestHourOffset;
        }
        if ($HourOffset != 0) {
            $SecondsOffset = $HourOffset * 3600;
            $Timestamp += $SecondsOffset;
            $Now += $SecondsOffset;
        }
        return $Timestamp;
    }