note 83356 added to function.timezone-offset-get

From: Date: Thu, 22 May 2008 10:02:55 +0000
Subject: note 83356 added to function.timezone-offset-get
Groups: php.notes 
Request: Send a blank email to php-notes+get-139604@lists.php.net to get a copy of this message
Hi, This might help someone with timezone differences. I wanted to find the offset in seconds between my timezone and a remote timezone so I wrote this function and it seems to work well for me. <?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; } ?> Examples: <?php // 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; ?> Happy Coding!! ---- Server IP: 64.71.164.2 Probable Submitter: 24.236.212.92 ---- Manual Page -- http://www.php.net/manual/en/function.timezone-offset-get.php Edit -- https://master.php.net/note/edit/83356 Del: integrated -- https://master.php.net/note/delete/83356/integrated Del: useless -- https://master.php.net/note/delete/83356/useless Del: bad code -- https://master.php.net/note/delete/83356/bad+code Del: spam -- https://master.php.net/note/delete/83356/spam Del: non-english -- https://master.php.net/note/delete/83356/non-english Del: in docs -- https://master.php.net/note/delete/83356/in+docs Del: other reasons-- https://master.php.net/note/delete/83356 Reject -- https://master.php.net/note/reject/83356 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#139604) next »