Date-related formatting is also a big part of internationalization, because different time zones and different countries have different representations of dates. Today we are going to focus on representing date-related information internationally.

Date formatting is first and foremost the most straightforward formatting capability.

$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); $FMT ->format(time()), PHP_EOL; // en_US formatting results: Friday, November 20, 2020 at 4:45:06 PM Pacific Standard Time $fmt = new IntlDateFormatter( "de-DE" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); Echo "de_DE formatting the results as follows:". $FMT - > format (IntlCalendar: : createInstance ()), PHP_EOL; // de_DE formatting results in: Freitag, 20. November 2020 UM 16:45:06 Nordamerikanische Westkusten -Normalzeit $FMT = new IntlDateFormatter(" zh-cn" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Shanghai',IntlDateFormatter::GREGORIAN ); $FMT ->format(time()), PHP_EOL; $FMT ->format(time()); The result of formatting zh-cn is: 8:45:06 AM CST, Saturday, November 21, 2020Copy the code

The IntlDateFormatter object is the operation class for date formatting in the internationalization component. The first parameter is the country locale. The second and third parameters are the date and day display formats, respectively, which we will demonstrate in the next section of code. The fourth parameter is the time zone setting and the fifth parameter is the time specification, in this case Gregorian time is specified.

Use the format() method to format a timestamp or calendar object with a date or time. It can only accept these two types of arguments and format them. It outputs according to the various parameters set by the IntlDateFormatter object, such as the output language is English, German, Chinese, etc., and the output time is in the time zone (8pm in China, 4pm in the US).

For date and time display formats, we can use several constants of IntlDateFormatter class, including FULL, SHORT, MEDIUM, and LONG.

$fmt = new IntlDateFormatter( "zh-CN" ,IntlDateFormatter::SHORT, IntlDateFormatter::LONG, 'Asia/Shanghai',IntlDateFormatter::GREGORIAN ); $FMT ->format(time()), PHP_EOL; $FMT ->format(time()); // zh-cn Formatting result: 2020/11/21 GMT+8 8:45:06 amCopy the code

In addition, the sixth argument to the constructor specifies the formatting rules for the formatting.

$fmt = new IntlDateFormatter( "zh-CN" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Shanghai',IntlDateFormatter::GREGORIAN, 'yyyy/MM/dd' ); $FMT ->format(time()), PHP_EOL; $FMT ->format(time()); // zh-cn The format result is 2020/11/21Copy the code

Format the date according to the specified object in the format() method above we saw that only timestamp and calendar object types can be used. There’s another, more powerful format method, and that’s the formatObject() method. As you can infer from the name, it formats the date data against the specified object.

$cal = IntlCalendar::createInstance(new DateTimeZone('Asia/Shanghai')); echo IntlDateFormatter::formatObject($cal),PHP_EOL; // Nov 21, 2020, 8:45:06 AM echo IntlDateFormatter::formatObject($cal, IntlDateFormatter::FULL),PHP_EOL; // Saturday, November 21, 2020 at 8:45:06 AM China Standard Time echo IntlDateFormatter::formatObject($cal, IntlDateFormatter::NONE, IntlDateFormatter::FULL),PHP_EOL; // 20201121 08:45 AM echo IntlDateFormatter::formatObject($cal, IntlDateFormatter::FULL, 'zh-CN'),PHP_EOL; / / on November 21, 2020, the Chinese standard time on Saturday morning 8:45:06 echo IntlDateFormatter: : formatObject ($CAL, "d 'of' MMMM y", 'useful - CN), PHP_EOL; // 21 of November 2020Copy the code

The most commonly used method is still the formatting of calendar objects. As you can see, the formatObject() method has more arguments. It can also directly specify the format of the date and time and the associated language Settings. In addition, it can specify rich output rules, such as the day of the month that our last piece of code outputs. We also used this method for testing in our article on the Internationalized calendar class in PHP. There are a lot of custom syntax rules, and you can check the ICU documentation for yourself.

In addition to formatting the calendar class, the formatObject() method also formats DateTime objects.

$dt = new DateTime();
echo IntlDateFormatter::formatObject($dt),PHP_EOL;
// Nov 21, 2020, 8:45:06 AM
Copy the code

Note, however, that formatObject() is very slow from the official documentation Note, about 10 times worse than the format() method under PHP5, and about 3 times worse under PHP7. So, if you don’t have a specific need, try not to use formatObject() to format dates and times.

We can format an object or timestamp to display in a standard string format. Can we reverse that standard string format data?

$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); $arr = $fmt->localtime($fmt->format(time())); print_r($arr); // Array // ( // [tm_sec] => 1 // [tm_min] => 59 // [tm_hour] => 16 // [tm_year] => 120 // [tm_mday] => 20 // [tm_wday] => 5 // [tm_yday] => 325 // [tm_mon] => 10 // [tm_isdst] => 0 // ) echo $fmt->parse("Thursday, November 19, 2020 at 5:05:41 PM Pacific Standard Time"), PHP_EOL; / / 1605834341Copy the code

The localTime () method is used to parse the content of a given standard date, printing the contents of the string in reverse as an array containing the year, month, day, hour, minute, second, and so on, according to the IntlDateFormatter initialization rules. The parse() method converts the given content directly to the corresponding timestamp.

$fmt = new IntlDateFormatter( "zh-CN" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'Asia/Shanghai',IntlDateFormatter::GREGORIAN ); $arr = $FMT -> localTime; $arr = $FMT -> localTime; print_r($arr); // Array // ( // [tm_sec] => 8 // [tm_min] => 54 // [tm_hour] => 8 // [tm_year] => 120 // [tm_mday] => 20 // [tm_wday] = > 5 / / [tm_yday] = > 325 / / [tm_mon] = > 10 / / [tm_isdst] = > 0 / /) echo $FMT - > parse (" on November 20, 2020 Chinese standard time on Friday morning 8:54:08 "), PHP_EOL; / / 1605833648Copy the code

Both English and Chinese are well supported.

For calendar types, there are only two types of calendar, GREGORIAN and TRADITIONAL, corresponding to the GREGORIAN and TRADITIONAL calendar respectively. We can specify the fifth parameter in the construct argument, or we can use the setCalendar() method while the object is in use. The getCalendar() method is used to get information about the date type currently set.

$fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo $fmt->getCalendar(), PHP_EOL; // 1 $fmt->setCalendar(IntlDateFormatter::TRADITIONAL); echo $fmt->getCalendar(), PHP_EOL; Date and time / / 0 type / / date to get and set $FMT = new IntlDateFormatter (" en_US ", IntlDateFormatter: : FULL, IntlDateFormatter: : FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo $fmt->getDateType(), PHP_EOL; // 0 $fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::SHORT, IntlDateFormatter::FULL, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo $fmt->getDateType(), PHP_EOL; Echo $FMT ->getTimeType(), PHP_EOL; // 0 $fmt = new IntlDateFormatter( "en_US" ,IntlDateFormatter::SHORT, IntlDateFormatter::MEDIUM, 'America/Los_Angeles',IntlDateFormatter::GREGORIAN ); echo $fmt->getTimeType(), PHP_EOL; / / 2Copy the code

For date and time types, we can only specify them as constructor arguments, and we get the values of the corresponding constants.

Regional language information

echo $fmt->getLocale(), PHP_EOL; // en
echo $fmt->getLocale(Locale::VALID_LOCALE), PHP_EOL; // en_US
Copy the code

I won’t explain this too much, as I’ve seen in previous articles, but it seems that classes that internationalize related components contain both methods.

We can specify formatting rules in the sixth argument of the constructor, and we can set the object dynamically.

echo $fmt->getPattern(), PHP_EOL; // M/d/yy, h:mm:ss a
$fmt->setPattern('yyyyMMdd hh:mm:ss z');
echo $fmt->getPattern(), PHP_EOL; // yyyyMMdd hh:mm:ss z
echo $fmt->format(time()), PHP_EOL; // 20201120 04:59:01 PST
Copy the code

After using setPattern() to set the formatting rules, formar() is formatted with the new formatting rules.

Timezone Settings Start with a getTimezoneId() method. It gets the time zone content directly, which is a string.

echo $fmt->getTimezoneId(), PHP_EOL; // America/Los_Angeles // $fmt->setTimeZoneId('CN'); Echo $FMT ->getTimezoneId(), PHP_EOL;Copy the code

However, the setTimezoneId() method has been removed in PHP7, and it is now recommended to use the setTimezone() method to set the time zone information, which we will look at in a moment.

var_dump($fmt->getTimezone());
// object(IntlTimeZone)#4 (4) {
//     ["valid"]=>
//     bool(true)
//     ["id"]=>
//     string(19) "America/Los_Angeles"
//     ["rawOffset"]=>
//     int(-28800000)
//     ["currentOffset"]=>
//     int(-28800000)
//   }

$fmt->setTimeZone('Asia/Shanghai');
var_dump($fmt->getTimezone());
// object(IntlTimeZone)#4 (4) {
//     ["valid"]=>
//     bool(true)
//     ["id"]=>
//     string(13) "Asia/Shanghai"
//     ["rawOffset"]=>
//     int(28800000)
//     ["currentOffset"]=>
//     int(28800000)
//   }

$fmt->setTimeZone('GMT+00:30');
var_dump($fmt->getTimezone());
// object(IntlTimeZone)#4 (4) {
//     ["valid"]=>
//     bool(true)
//     ["id"]=>
//     string(9) "GMT+00:30"
//     ["rawOffset"]=>
//     int(1800000)
//     ["currentOffset"]=>
//     int(1800000)
//   }
Copy the code

Unlike getTimezoneId(), getTimezone() returns an IntlTimeZone object. The official documentation for this object is incomplete. Many of the method parameters are not written, so I won’t write about it. You can look it up on your own. For simple time zone Settings, however, the setTimezone() method takes a string as an argument. For example, the code above changes the time zone of Los Angeles, USA to Shanghai, China and GMT+00:30 respectively. Similarly, if we format() to output the time, it is the standard time in the current time zone.

We do a lot of work with calendar objects when we format data, and we can get calendar information from IntlDateFormatter as well.

$cal = $fmt->getCalendarObject();
var_dump(
    $cal->getType(),
    $cal->getTimeZone(),
    $cal->getLocale(Locale::VALID_LOCALE)
);
// string(9) "gregorian"
// object(IntlTimeZone)#3 (4) {
//   ["valid"]=>
//   bool(true)
//   ["id"]=>
//   string(9) "GMT+00:30"
//   ["rawOffset"]=>
//   int(1800000)
//   ["currentOffset"]=>
//   int(1800000)
// }
// string(5) "en_US"
Copy the code

Tolerance and finally tolerance, which is a strict mode of operation. For example, if we define an error time, the operation in IntlDateFormatter does not report an error because it is tolerant by default.

$fmt->setPattern('dd/mm/yyyy'); var_dump($fmt->isLenient()); // bool(true) echo $fmt->parse('35/13/1955'), PHP_EOL; / / - 470449020Copy the code

Obviously, this date is the wrong date. Using the isLenient() method, we can get the current state of whether we are tolerant or not. Now let’s take the tolerance away and see what happens.

$fmt->setLenient(FALSE);
echo $fmt->parse('35/13/1955'), PHP_EOL;
// 

echo $fmt->getErrorCode(), PHP_EOL; // 9
echo $fmt->getErrorMessage(), PHP_EOL; // Date parsing failed: U_PARSE_ERROR
Copy the code

The parse() method has no output. We also saw error messages through getErrorCode() and getErrorMessage(). This is the main ability to tolerate processing in the IntlDateFormatter object.

So we’re going to look at IntlDateFormatter, and we’re going to look at IntlDateFormatter. Number and date formats are the most important functions related to internationalization, and can be applied to our daily business development at any time. You can learn more about relevant knowledge.