3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** Returns the offset from the origin timezone to the remote timezone, in seconds. * @param $remote_tz; * @param $origin_tz; If null the servers current timezone is used as the origin. * @return int; */ function get_timezone_offset($remote_tz, $origin_tz = null) { if($origin_tz === null) { if(!is_string($origin_tz = date_default_timezone_get())) { return false; // A UTC timestamp was returned -- bail out! } } $origin_dtz = new DateTimeZone($origin_tz); $remote_dtz = new DateTimeZone($remote_tz); $origin_dt = new DateTime("now", $origin_dtz); $remote_dt = new DateTime("now", $remote_dtz); $offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt); return $offset; } // This will return 10800 (3 hours) ... $offset = get_timezone_offset('America/Los_Angeles','America/New_York'); // or, if your server time is already set to 'America/New_York'... $offset = get_timezone_offset('America/Los_Angeles'); // You can then take $offset and adjust your timestamp. $offset_time = time() + $offset; ?>
Output for 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.10, 7.2.0
Output for 5.0.0 - 5.0.5, 5.1.0 - 5.1.6
Fatal error: Class 'DateTimeZone' not found in /in/tnI2q on line 13
Process exited with code 255.
Output for 4.3.2 - 4.3.11, 4.4.0 - 4.4.9
Fatal error: Cannot instantiate non-existent class: datetimezone in /in/tnI2q on line 13
Process exited with code 255.
Output for 4.3.0 - 4.3.1
Fatal error: Cannot instantiate non-existent class: datetimezone in /in/tnI2q on line 13

preferences:
181.82 ms | 401 KiB | 215 Q