Tag Archives: offset

PHP: Identify the Offset of a Specific Array Element CodeUnit 04 DEC 2012

To learn the offset or array key for a specific array element in a given array turns out to be fairly easy, thanks to the extra search parameter that the always useful array_keys (return all the keys or a subset of the keys of an array) function offers us.

From the manual description: array array_keys ( array $input [, mixed $search_value = NULL [, bool $strict = false ]] ) – array_keys() returns the keys, numeric and string, from the input array. If the optional search_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the input are returned.

Knowing this, we can now find exactly where a specific element sits by simply entering a search value when calling the array_keys function, which in practice would look something like this:

$array = array("blue", "red", "green", "blue", "blue");
print_r(array_keys($array, "blue"));
/*
Array
(
    [0] => 0
    [1] => 3
    [2] => 4
)
*/

The above example shows us that the array element “blue” appears at offset 0, 3 and 4 in the source array.

Nifty.

Related Link: http://www.php.net/manual/en/function.array-keys.php

PHP: Determine the Offset between two Time Zones Programming 07 SEP 2009

Because I keep forgetting where I put this code fragment on my system, grabbed from the comments section from the PHP online manual just by the way, I may as well just pop it on here so that I can keep track of it better in the future.

The function you see below is useful for determining the offset (in seconds) between your local (origin) timezone and a remote timezone. In order to run, it requires a remote timezone region string, as well as a second timezone region string if you don’t wish to use the system’s preset timezone. (And if you’re looking for timezone regions strings, may I suggest the excellent http://www.worldtimezones.com as a source?)

So, without further ado, here’s the nifty code:

/**    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;
}

And what good would the code be without a nice example to go along with it:

// 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;

It’s clean, it works and I like it, so kudos to the person named Dan at authenticdesign.net then! :)

wall clocks for different time zones