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 :-)
By: Derick Rethans
Url:
www.derickrethans.nl/talks.phpContent
')
Time zone problems- Most places on earth have an hour shift.
- Some places have a half-hour or fifteen-minute temporary offset.
- In some places, the time zone changes throughout the year.
Time translation problems- Many countries translate time in order to increase the duration of the light part of the day.
- Not all countries do this.
- Clock transfer occurs at different times
- There are many exceptions.
Other problems of time zones and “switch hands”- In Australia, the zones are strictly vertical.
- In Brazil, the date when they translate the earrings changes every year.
- On the island of Lord Howe (Australia) translate the arrows only on the hour
- Nepal translates the hands to 15 minutes
Problems in naming time zones- One naming can mean different time zones:
For example: PST - Pacific Standart Time and Pakistan Standart Time
EST - Easten Standart Time (USA), Easten Standart Time (Australia) and Easten Brazil Standart Time - One zone can have several names.
Moreover, in different operating systems, zone names may vary.
Problems in the presentation of date and time- Ambiguity
06/08/04
August 6, 2004
June 8, 2004
August 4, 2006 - Unreadability
20040425010541
April 25, 2004 01:05:41 - Other "magic" formats
third saturday
2004-03-10 16: 33: 17.11403 + 1
2001-11-29T13: 20: 01.123-05: 00
23: 41: 00.0Z
04: 05: 07.789 +0930
1999.238
Functions in php 4 and php 5 for working with time and dates- checkdate - Checks the correctness of the date on the Gregorian calendar.
- date (gmdate) - Formats the system date / time (GMT)
- getdate - Returns date / time information
- gettimeofday - Returns the current time.
- localtime - Returns the system time
- mktime (gmmktime) - Returns the timestamp for the given date (GMT)
- strftime (gmstrftime) - Formats the date / time according to the current locale (GMT)
- strtotime - Converts a textual representation of a date in English to a Unix timestamp.
Date Restrictions- Using Unix-timestamp as a base (number of seconds since January 1, 1970 GMT)
- The timestamp is represented as a 32-bit integer, which limits the use of this representation to the range of 1902-2038
- On many operating systems, this type can only be positive, limiting the range 1970-2038
- strtotime is a fairly complex function, the implementation of which is subject to a large number of errors
- No options for correct work with time zones
- Some features are operating system dependent.
Date handling improvements in php 5.1 and up- Using the 64-bit type to store the date. Now the boundary of the interval of use of this type 4 December 292277026596 year
- The strtorime function has been rewritten.
- There is no more dependence on the implementation of the operating system
- Full support for time zones, clock translation and date modifiers
- New time zone modifier format
- More advanced functions for working with dates
Data formats in php 5.1 and higherAmerican 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 above- Built-in base for 551 time zones
- No dependency on time zone abbreviation
- Time zone names have the format - Continent / Location or Continent / Location / Sublocation
Time zone support. How to use?- Each time zone is identified by the city with the largest population in the area.
- The zones are divided into 10 large groups: Africa, America, Antarctic, Arctic, Asia, Europe, India, Pacific
- There is also a group Other, consisting of obsolete names and serving to ensure backward compatibility
- More information on the abbreviations of time zones at the specified address
Change information on time zones- Information updates on time zones occur about 20 times a year.
- Most updates happen unexpectedly.
- In order not to lose relevance, php releases should be quite frequent.
- The pecl extension that fixes these flaws is called timezonedb (pecl install timezonedb)
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 formatdate_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 php6date_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 timedate_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 zonesSetting 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 zoneSetting 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 value set by the date_default_timezone_set () function
- environment variable tz
- date.timezone value in php.ini
- system display abbreviation of time zone name
The use of time zones. Object time zoneCreating 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 changeUsing 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 identifiersGetting 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 appearIn php 5.1 you need to compile using the flag CFLAGS = -DEXPERIMENTAL_DATE_SUPPORT = 1
In php 5.2 and higher by default
Additionally posted
here.