Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: setting a timezone by UTC offset

Using javascript I know that my users timezone is UTC +3.

Now I want to create DateTime object with this knowledge:

$usersNow = new DateTime('now', new DateTimeZone("+3")); 

I receive as a respsonse:

'Unknown or bad timezone (+2)' 

What am I doing wrong? How can I fix?

like image 820
shealtiel Avatar asked Sep 01 '11 20:09

shealtiel


People also ask

How do you write time with UTC offset?

This difference is expressed with respect to UTC and is generally shown in the format ±[hh]:[mm], ±[hh][mm], or ±[hh]. So if the time being described is two hours ahead of UTC (such as in Kigali, Rwanda [approx. 30° E]), the UTC offset would be "+02:00", "+0200", or simply "+02".

What timezone is UTC for php?

The default timezone for PHP is UTC regardless of your server's timezone. This is the timezone used by all PHP date/time functions in your scripts. See PHP's list of supported timezones to find the names of all possible timezones you can use for the date.

How do you offset time zones?

The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC. For example, the value 08:00-08:00 represents 8:00 AM in a time zone 8 hours behind UTC, which is the equivalent of 16:00Z (8:00 plus eight hours). The value 08:00+08:00 represents the opposite increment, or midnight (08:00 minus eight hours).


2 Answers

how about this...

$original = new DateTime("now", new DateTimeZone('UTC')); $timezoneName = timezone_name_from_abbr("", 3*3600, false); $modified = $original->setTimezone(new DateTimezone($timezoneName)); 
like image 189
minaz Avatar answered Sep 25 '22 09:09

minaz


You said:

Using javascript I know that my users timezone is UTC +3.

You probably ran something like this:

var offset = new Date().getTimezoneOffset(); 

This returns the current offset from UTC in minutes, with positive values falling west of UTC. It does not return a time zone!

A time zone is not an offset. A time zone has an offset. It can have multiple different offsets. Often there are two offsets, one for standard time and one for daylight saving time. A single numeric value cannot represent this alone.

  • Example of a time zone: "America/New_York"
    • Corresponding standard offset: UTC-5
    • Corresponding daylight offset: UTC-4

Besides the two offsets, also wrapped up in that time zone are the dates and times for transitioning between the two offsets so you know when they apply. There's also a historical record of how the offsets and transitions may have changed over time.

See also "Time Zone != Offset" in the timezone tag wiki.

In your example case, you probably received a value of -180 from javascript, representing a current offset of UTC+3. But that's just the offset for that particular point in time! If you follow minaz's answer, you will get a time zone that makes the assumption that UTC+3 is always the correct offset. That would work if the real time zone is something like "Africa/Nairobi" which has never used anything except UTC+3. But for all you know your user could be in "Europe/Istanbul", which uses UTC+3 in the summer and UTC+2 in the winter.

like image 45
Matt Johnson-Pint Avatar answered Sep 23 '22 09:09

Matt Johnson-Pint