$timezone = new DateTimeZone(date_default_timezone_get()); $offset = $timezone->getOffset(new DateTime('now')->getTimestamp()); echo $offset; // output: -14400 (-4 hours)
$timezone = new DateTimeZone('Europe/Paris'); $date_time = new DateTime('2021-01-01 12:00:00', $timezone); $offset = $timezone->getOffset($date_time->getTimestamp()); echo $offset; // output: 3600 (+1 hour)This code creates a new DateTimeZone object representing the timezone of Paris and a DateTime object representing January 1st, 2021 at noon. The getOffset function is called passing in the timestamp of the DateTime object. The output will be the timezone's offset from UTC at that moment in seconds. The DateTimeZone class is part of the PHP core library, so it doesn't require any additional package installation.