-1

I search the better solution for view the local time for all users of my website.

I got a problem with summer time, actually i got this function if you got a better way help me :

function timezone_by_offset($offset) {


 $abbrarray = timezone_abbreviations_list();
    $offset = $offset * 60 * 60;

    foreach ($abbrarray as $abbr) {
        foreach ($abbr as $city) {
            if ($city['offset'] == $offset && $city['dst'] == TRUE) { 
                return $city['timezone_id'];                                
            }
        }
    }
    return 'UTC'; // any default value you wish
}
echo timezone_by_offset(1) . '<br/>';  
echo (date_default_timezone_set(timezone_by_offset(1)) == TRUE ? 'Valid'. date('Y-m-d H:i:s') : 'Not Valid'). '<br/>';

SOLUTION :

<?php
 function get_timezones() 
 {
    $o = array();
    $t_zones = timezone_identifiers_list();
    foreach($t_zones as $a)
    {
        $t = '';

        try
        {
            //this throws exception for 'US/Pacific-New'
            $zone = new DateTimeZone($a);

            $seconds = $zone->getOffset( new DateTime("now" , $zone) );
            $hours = sprintf( "%+02d" , intval($seconds/3600));
            $minutes = sprintf( "%02d" , ($seconds%3600)/60 );

            $t = $a ."  [ $hours:$minutes ]" ;

            $o[$a] = $t;
        }

        //exceptions must be catched, else a blank page
        catch(Exception $e)
        {
            //die("Exception : " . $e->getMessage() . '<br />');
            //what to do in catch ? , nothing just relax
        }
    }

    ksort($o);

    return $o;
} 

$o = get_timezones();
?>

<html>
<body>
<select name="time_zone">
<?php
    foreach($o as $tz => $label)
    {
        echo "<option value="$tz">$label</option>";
    }
?>
</select>
</body>
</html>

Best Regards.

Rodo
  • 3
  • 4

1 Answers1

0

UPDATE 3
PART 1 - Finding correct timezone
If you are looking for a timezone_name with help of its offset and HAS daylight saving you set the the third parameter of timezone_name_from_abbr() to true, otherwise to false. If you set it to 1 and the offset + daylight saving does not give valid timezone_name it will return false. In such cases you can turn it back to false.
Example

$tz = timezone_name_from_abbr(null, $offset * 3600, true);
if($tz === false) $tz = timezone_name_from_abbr(null, $offset * 3600, false);

PART 2 - Get local date/time accoring to DAYLIGHT SAVINGS
You do not have to do anything here, once timezone is set PHP date function automatically sets daylight savings according to your timezone and CURRENT date. So

function print_according_to_offset($offset)
{

    $tz = timezone_name_from_abbr(null, $offset, true); //If a ofset with daylight savings is found
    if($tz === false) $tz = timezone_name_from_abbr(null, $offset, false); //otherwise
    if($tz === false) // If no timezone is still found
    {
        echo 'Invalid Offset <br />';
        return;
    }
    $otherTZ  = new DateTimeZone($tz); 
    $datetime = new DateTime; // current time = server time
    $datetime->setTimezone($otherTZ);
    echo $tz . '<br />';
    echo $datetime->format('Y-m-d H:i:s') .'<br /><br />';
}

print_according_to_offset(19800);

UPDATE 2

<?php
$timezone = '+0:00';
$timezone = preg_replace('/[^0-9]/', '', $timezone) * 36;
$timezone_name = timezone_name_from_abbr(null, $timezone, true);
date_default_timezone_set($timezone_name);
echo date('D d M Y H:i:s');
?>

More information
UPDATE
CI function and improved code

function print_according_to_offset($offset)
{
    $user_timezone =  timezone_name_from_abbr("",$offset,0);
    $otherTZ  = new DateTimeZone($user_timezone);
    $datetime = new DateTime; // current time = server time
    $datetime->setTimezone($otherTZ);
    echo $datetime->format('Y-m-d H:i:s') .'<br />';
}

print_according_to_offset(14400); //EXAMPLE

Dont expect us to do everything for you, edit the function and return $otherTZ if you need to get timezone only.
Old Post
I think this is what you need.

$offset = 3600 ; //your offset
$my_timezone =  timezone_name_from_abbr("",$offset,0);

date_default_timezone_set($my_timezone);
echo date('Y-m-d H:i:s');

More info

Varun Garg
  • 2,464
  • 23
  • 37
  • The timezone is not fix, my users on my website is international, i use codeigniter framework and i got a function for get timezone (UTC+1, UTC+2, UTC+3, etc) but i don't know how do this corrrectly – Rodo May 28 '15 at 17:50
  • It is in php and CI is based on php. It will definitely run with CI, all you need is to set the offset. But I have updated the solution so you can implement easily with CI – Varun Garg May 28 '15 at 18:16
  • so your system does the same thing as me except that my problem is offset, I can't know what has offset corresponds to UTC +1 for example – Rodo May 28 '15 at 18:50
  • I thought you had the found the correct offset. I have updated my post, it should work now. – Varun Garg May 29 '15 at 05:18
  • Ok but the problem with this way is for the summer time and winter time, i know in west european the time is UTC +1 in winter and UTC +2 in summer and i don't know how do that and your function doestn't work correctly cause the summer time is not take for all country in the world and i can't do UTC -1, UTC -2, etc – Rodo May 29 '15 at 07:08
  • I have found better solution of you, but a problem persist for convert a date in my database with timezone user got – Rodo May 29 '15 at 15:37