Вопрос

I want to leave the option whether to follow DST or not to user's choice. I was trying CST and CST6CDT timezones but bothare giving me same result.

php > $tz = new DateTimeZone('CST');
php > 
php > $d = new DateTime('now', $tz);
php > print_r($d);
DateTime Object
(
    [date] => 2013-10-16 08:34:53
    [timezone_type] => 3
    [timezone] => America/Chicago
)
php > 
php > $tz = new DateTimeZone('CST6CDT');
php > 
php > $d = new DateTime('now', $tz);
php > print_r($d);
DateTime Object
(
    [date] => 2013-10-16 08:35:21
    [timezone_type] => 3
    [timezone] => CST6CDT
)
php > 

Is there any way I can get a timestamp without DST? Am I doing anything wrong here?

Это было полезно?

Решение

CST right now IS in DST (except me, being in Saskatchewan where we don't observe DST). So CST and CDT are technically the same time right now. If you'd tried this again in mid-November once the DST switch occurs, you would get different times.

Try GMT-6 instead, which will get you a cst-type time, regardless of DST, because the GMT zones are not DST-dependent.

Другие советы

PHP uses the IANA standard time zone database. In this data, values such as "CST" and "CST6CDT" only exist for backwards compatibility with POSIX time zones. You should not use them unless absolutely necessary.

The PHP documentation here makes this very clear:

Warning

Please do not use any of the timezones listed here (besides UTC), they only exist for backward compatible reasons.

You should instead be using "America/Chicago" to represent the US Central Time Zone.

You can read more about the IANA time zone database in the timezone tag wiki.

Part of the reason why "CST" is a bad choice for a time zone identifier, is that time zone abbreviations can be ambiguous. "CST" might stand for "Central Standard Time", but that could be USA Central Time, or Australian Central Time. It could also stand for "Cuba Standard Time" or "China Standard Time".

Even if you know that it is in the USA, "CST6CDT" is not a very good identifier, because it doesn't take into account that the DST rules for the USA changed in 2007.

Regarding your comment:

I want to leave the option whether to follow DST or not to user's choice.

That is a code smell. Any time you see a checkbox on a web site for DST [yes/no], that is a clear sign that the application is not using time zones properly. When your user selects a time zone of "America/Chicago" or "America/Los_Angeles", or "Europe/London", etc. The information in the time zone database will already know whether or not to follow DST and when. DST starts and stops at many different times all over the world, so it is essential that you let the database be used properly.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top