⬆️ ⬇️

Working with temporary zones in PHP

As soon as the project ceases to be tied to a limited number of consumers and the geography of its application grows, the question arises about the use of time zones. When I worked at a well-known Internet company, the introduction of time zones into work (both in the statistics display interface and in the analysis program) was quite a serious step.



Next translation :-)



Time zone problems

Time translation problems

Other problems of time zones and “switch hands”

Problems in naming time zones

Moreover, in different operating systems, zone names may vary.

')

Problems in the presentation of date and time

Functions in php 4 and php 5 for working with time and dates

Date Restrictions

Date handling improvements in php 5.1 and upData formats in php 5.1 and higher

American format: 9/11; 4: 08 pm; 12/22/78; 8:51:00 am

Combined format: Sat, 24 Apr 2004 21:48:40 +0200; 2001-11-29T13: 20: 01.123-05: 00

Descriptive format: tomorrow; four months ago; last saturday; +20 days 2 hours

Text format: December 22. 1978; 22-december-1978

All ISO 8601 formats: 1978/12/22; 13: 03: 12.45678; 13: 03: 12.45678 +0100; 15: 57-8; 1978-12-22; 15:57:41 pdt; 13: 03: 12.45678 CEST; 231431 CEST; 70-4-25; 13:03 CEST; 04:05 -0930; 23: 41F

Database Formats: 1999-Jan-08; 1999.238



Time zone support in php 5.1 and aboveTime zone support. How to use?

Change information on time zones

Parsing dates (past)

Parsing strings from date and time information using the strtotime function

$ ts = strtotime ("2005-07-11 22:16:50 CEST");


Using timestamp initialization

$ date = strtotime ("2005-07-11 22:16:50 CEST");

$ ts = strtotime ("next week", $ date);


The return value is a 32-bit integer.

Parsing dates (future)

Parsing a string containing date and time information using the date_create function

"$ Ts = date_create (" 1978-12-22 09:15:50 ");"

Alternatively, you can build a DateTime object.

$ ts = new DateTime ("1978-12-22 09:15:50");


These functions no longer use a 32-bit integer, they return a DateTime object, which is a wrapper over a 64-bit integer, for access to which you should use the following construct

$ ts = new DateTime ("1978-12-22 09:15:50");

echo $ ts-> format ('U');

Parsing a string using the date_parse function

$ date = "22apr2006 8: 36: 14.43 # ^ Europe / Oslo CEST";

$ t = date_parse ($ date);

echo $ t ['year'] .'- '. $ t [' month '] .'-'. $ t ['date']. ' ';

echo $ t ['hour']. ':'. $ t ['minute']. ':'. $ t ['second']. ' ';

echo $ t ['tz_id']. "\ n";



Date format

date_default_timezone_set ("Europe / Oslo");

$ ts = date_create ("1979-12-31 09:15");

echo date_format ($ ts, "D Ymd H: i: s - \ I \ S \ O \ W / \ Y: W / o");



All format modifiers supported by the date function are also supported.

Predefined formats:

date_default_timezone_set ("Europe / Oslo");

$ ts = date_create ("December 22nd, 2005 15:41");

echo date_format ($ ts, DATE_ISO8601);

echo date_format ($ ts, DateTime :: RFC1036);

echo date_format ($ ts, DATE_RSS);



Locale in php6

date_default_timezone_set ("Europe / Oslo");

$ ts = date_create ("December 22nd, 2005 15:41");

$ locales = array ('en_US', 'fr_CA', 'nb_NO', 'ru_RU', 'ar_SA', 'ja_JP');

foreach ($ locales as $ locale)

{

locale_set_default ($ locale);

echo date_format_locale ($ ts, DATE_RFC2822). "\ n";

}



Update date and time

$ date = new DateTime ('now');

echo $ date-> format (DateTime :: ISO8601). "\ n";

$ date-> setTime (15,0,7);

echo $ date-> format (DateTime :: ISO8601). "\ n";

$ date-> setDate (2006,12,22);

echo $ date-> format (DateTime :: ISO8601). "\ n";

$ date-> setIsoDate (2006,45,2);

echo $ date-> format (DateTime :: ISO8601). "\ n";



Change date and time

date_default_timezone_set ("Europe / Oslo");

$ date = new DateTime ('now');

echo $ ts-> format (DATE_RFC2822). "\ n";

$ ts-> modify ("+ 2 days");

echo $ ts-> format (DATE_RFC2822). "\ n";

$ ts-> modify ("fifth month");

echo $ ts-> format (DATE_RFC2822). "\ n";

$ ts-> modify ("Friday +3 weeks");

echo $ ts-> format (DATE_RFC2822). "\ n";

$ ts-> modify ("next friday");

echo $ ts-> format (DATE_RFC2822). "\ n";



Using time zones

Setting the abbreviated name of the time zone when parsing the date

$ ts = date_create ("1978-12-22 09:15 CEST");



The use of time zone abbreviations is considered obsolete, it is necessary to set a default time zone or full time zone identifier

$ ts = date_create ("1978-12-22 09:15 Europe / Oslo")


Default time zone

Setting the default time zone

date_default_timezone_set ("Europe / Oslo");

$ ts = date_create ("1978-12-22 09:15");

echo $ ts-> format ('e');



Get default time zone

echo date_default_timezone_get ();


The default time zone is determined according to

The use of time zones. Object time zone

Creating a time zone

$ tz = timezone_open ('Asia / Singapore');


Using time zone when parsing string representation

$ tz = timezone_open ('Pacific / Honolulu');

$ ts = date_create ("1978-12-22 09:15 ', $ tz);



Using the time zone as a function argument does not overlap the time zone specified in the line to be parsed

$ tz = new DateTimeZone ('Pacific / Honolulu');

$ ts1 = new DateTime ('1978-12-22 09:15 CEST', $ tz);

$ ts2 = new DateTime ('1978-12-22 09:15 Europe / Amsterdam', $ tz);

echo $ ts2-> format (DateTime :: RFC2822);



Getting time zone name

$ tz = timezone_open ('Asia / Singapore');

echo timezone_name_get ($ tz);



$ tz = timezone_open ('CEST');

echo timezone_name_get ($ tz);



Getting the current offset from Greenwich

$ tz = new DateTimeZone ("Europe / Amsterdam");

$ d = new DateTime ("2005-01-22 09:15");

echo $ tz-> getOffset ($ d);

$ d-> modify ("+ 6 months");

echo $ tz-> getOffset ($ d);



The use of time zones. Time zone change

Using time zone when parsing string representation

$ tz1 = timezone_open ('Pacisic / Honolulu');

$ tz2 = timezone_open ('Europe / Amsterdam');

$ tz3 = timezone_open ('Australia / Melbourne');



$ ts = date_create ("1978-12-22 09:15", $ tz1);

echo $ ts-> getTimezone () -> getName (). ':'. $ ts-> format (DATE_RFC822). "\ n";

$ ts-> setTimezone ($ tz2);

echo $ ts-> getTimezone () -> getName (). ':'. $ ts-> format (DATE_RFC822). "\ n";

$ ts-> setTimezone ($ tz3);

echo $ ts-> getTimezone () -> getName (). ':'. $ ts-> format (DATE_RFC822). "\ n";



Time Translation Tables

$ tz = timezone_open ('Europe / Amsterdam');

$ trs = timezone_transitions_get ($ tz); // or $ trs = $ tz-> getTransitions ();

foreach ($ trs as $ ts)

printf ("% 20s% 7d% d% s \ n", $ tr ['time'], $ tr ['offset'], $ tr ['isdst'], $ tr ['abbr']);



Abbreviations and identifiers

Getting all supported ids

$ ids = timezone_identifiers_list ();

echo "Number of identifiers:" .count ($ ids). "\ n";

echo implode (",", array_slice ($ ids, 0, 5)) .'... '. implode (",", array_slice ($ ids, -5));



Getting all supported abbreviations

$ abbrs = timezone_abbreviations_list ();

foreach ($ abbrs as $ abbr => $ ids)

foreach ($ ids as $ id)

printf ("% - 6s% 6d% d% s \ n", strtoupper ($ abbr), $ id ['offset'], $ id ['dst'], $ id ['timezone_id']);



Arrow translation. When will the next one be?

date_default_timezone_set ("America / New_york");

$ tz = new DateTimeZone ("America / New_york");

foreach (timezone_transitions_get ($ tz) as $ tr)

if ($ tr ['ts']> time ()) break;

$ d = new DateTime ("@ {$ tr ['ts']}");

printf ("The timezone% s switches to% s on% s. \ nThe new GMT offset will be:% d (% s) \ n", $ tz-> getName (), $ tr ['isdst']? " DST ”:“ standard time ”, $ d-> format ('d MY @ H: i T'), $ tr ['offset'], $ tr ['abbr']);



When will this functionality appear

In php 5.1 you need to compile using the flag CFLAGS = -DEXPERIMENTAL_DATE_SUPPORT = 1

In php 5.2 and higher by default

Source: https://habr.com/ru/post/31405/



All Articles