This is the 14th day of my participation in the August Text Challenge.More challenges in August

In PHP’s internationalization component, another date-related action class that we don’t use very often is the calendar action class. Calendar, in fact, most of the date and time operation, generally also mainly used for date formatting and comparison and so on. However, we usually use date-related functions or datetime-related classes directly to manipulate date-related functions, which is more convenient and flexible than the calendar functions. Of course, for learning purposes, let’s look at it briefly.

Formatting time

Let’s start with formatting time.

$cal = IntlCalendar::createInstance(IntlTimeZone::getGMT());
var_dump(get_class($cal), IntlDateFormatter::formatObject($cal, IntlDateFormatter::FULL));
// string(21) "IntlGregorianCalendar"
// String (66) "Wednesday, 18 November 2020 12:58:14 AM GMT"

$cal1 = IntlCalendar::fromDateTime('2013-02-28 00:01:02 Europe/Berlin');
var_dump(get_class($cal1), IntlDateFormatter::formatObject($cal1.'yyyy MMMM d HH:mm:ss VVVV'.'de_DE'));
// string(21) "IntlGregorianCalendar"
// string(41) "2013 Februar 28 00:01:02 Deutschland Zeit"
Copy the code

The createInstance() method of the IntlCalendar class returns an IntlCalendar object with optional arguments that must be of type TimeZone. The fromDateTime() method also generates an IntlCalendar object, but it can set either a DateTime object or a string of date type as an argument.

As you can see, the object we returned using get_class() actually returns an IntlGregorianCalendar Greengli calendar object. The output can then be formatted using the formatObject() method of the IntlDateFormatter class, which can specify locale. Different locale Settings will display different formatting language results.

Return time stamp

echo IntlCalendar::getNow(), PHP_EOL; / / 1605661094417
Copy the code

Without further explanation, this static method returns a timestamp with a number of milliseconds.

Time Zone Settings

Anything related to internationalization is more or less TimeZone dependent, and the calendar class is no exception.

ini_set('intl.default_locale'.'de_DE');
ini_set('date.timezone'.'Europe/Berlin');
$cal = IntlCalendar::createInstance();
print_r($cal->getTimeZone());
// IntlTimeZone Object
/ / (
// [valid] => 1
// [id] => Europe/Berlin
// [rawOffset] => 3600000
// [currentOffset] => 3600000
// )

echo $cal->getLocale(Locale::ACTUAL_LOCALE), PHP_EOL; // de
echo $cal->getLocale(Locale::VALID_LOCALE), PHP_EOL; // de_DE
Copy the code

Use getTimeZone() to get the current time zone information. GetLocale () is no different from the getLocale() method used by other related functionality classes in this article. Of course, the TimeZone property can also be modified directly through the object’s setTimeZone() method in addition to ini_set().

ini_set('intl.default_locale'.'zh_CN');
ini_set('date.timezone'.'Asia/Shanghai');
$cal = IntlCalendar::createInstance();
print_r($cal->getTimeZone());
// IntlTimeZone Object
/ / (
// [valid] => 1
// [id] => Asia/Shanghai
// [rawOffset] => 28800000
// [currentOffset] => 28800000
// )

$cal->setTimeZone('UTC');
print_r($cal->getTimeZone());
// IntlTimeZone Object
/ / (
// [valid] => 1
// [id] => UTC
// [rawOffset] => 0
// [currentOffset] => 0
// )

echo $cal->getLocale(Locale::ACTUAL_LOCALE), PHP_EOL; // zh
echo $cal->getLocale(Locale::VALID_LOCALE), PHP_EOL; // zh_Hans_CN
Copy the code

Calendar related operations

Information about the maximum and minimum value of the time field

What does that mean? Let’s take a look at the code.

$cal = IntlCalendar::fromDateTime('2020-02-15');
var_dump($cal->getActualMaximum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 29
var_dump($cal->getMaximum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 31
var_dump($cal->getActualMinimum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 1
var_dump($cal->getMinimum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 1
var_dump($cal->getLeastMaximum(IntlCalendar::FIELD_DAY_OF_MONTH));/ / 28

$cal->add(IntlCalendar::FIELD_EXTENDED_YEAR, -1);
var_dump($cal->getActualMaximum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 28
var_dump($cal->getMaximum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 31
var_dump($cal->getActualMinimum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 1
var_dump($cal->getMinimum(IntlCalendar::FIELD_DAY_OF_MONTH)); / / 1
var_dump($cal->getLeastMaximum(IntlCalendar::FIELD_DAY_OF_MONTH));/ / 28
Copy the code

What the hell is all this upstairs? These methods are the maximum and minimum values returned for the specified parameter fields, such as FIELD_DAY_OF_MONTH, which is the number of days in a month. GetActualMaximum () returns the actual value, such as February 2020, which has 29 days. GetMaximum () returns the maximum value of a normal month, which is 31. GetActualMinimum () and getMinimum() return the actual minimum and the normal minimum, which are both 1 for the month, and each month must have a first day. The getLeastMaximum() method gets the minimum local maximum value for a field. February has a minimum of 28 days and a local maximum of 28 days. Other months have 30 and 31 days.

The start date of the week

This function allows you to set the start date of the week. For example, Monday is not the start of the week, but Sunday is the first day of the week. You can see this in all kinds of calendar apps.

$cal = IntlCalendar::createInstance();
$cal->set(2020.5.30);
var_dump($cal->getFirstDayOfWeek()); / / 1
echo IntlDateFormatter::formatObject($cal.<<
), PHP_EOL;
// local day of week: 3
// week of month : 5
// week of year : 27
Copy the code

In the current time zone, we get getFirstDayOfWeek() to return 1, meaning Monday is the starting point of the week and the day of the week is calculated from 0. The set() method sets a specific date, noting that months also start at 0. We’ll use IntlDateFormatter: : formatObject () outputs the current date in several weeks, how many weeks in the month and the current week is how many weeks of this year. In this case, we set the date to June 30, 2020, ‘cc’ means the current date is Thursday, the fourth day of the week, the current week is the fifth week of the current month, and the 27th week of the year. What if we changed the start time of the week?

$cal->setFirstDayOfWeek(3);
var_dump($cal->getFirstDayOfWeek());  // int(5)
echo IntlDateFormatter::formatObject($cal.<<
), PHP_EOL;
// local day of week: 1
// week of month : 6
// week of year : 27
Copy the code

Well, ‘cc’ has changed to 1, so it’s currently Monday. It’s now the sixth week of the current month, because we started the week on Thursday.

The calendar is

Calendar object comparison

$cal1 = IntlCalendar::createInstance();
$cal2 = IntlCalendar::createInstance();
var_dump($cal1->equals($cal2)); // bool(true)
$cal2->setTime($cal1->getTime() + 1);
var_dump($cal1->equals($cal2)); // bool(false)
Copy the code

The calendar object has different internal attributes. Of course, equals() returns false.

Calendar object difference

In addition to comparing calendar objects, you can also get information about the difference between two calendar times ago.

$cal1 = IntlCalendar::fromDateTime('the 2019-1-29 09:00:11');
$cal2 = IntlCalendar::fromDateTime('the 2020-03-01 09:19:29');
$time = $cal2->getTime();

echo "Previous time:", IntlDateFormatter::formatObject($cal1), "\n";
// Previous time: 9:00:11 am, Jan 29, 2019

printf(
    "%d year(s), %d month(s),"
  . "%d day(s), %d hour(s) and %d minute(s)\n".$cal1->fieldDifference($time, IntlCalendar::FIELD_YEAR),
    $cal1->fieldDifference($time, IntlCalendar::FIELD_MONTH),
    $cal1->fieldDifference($time, IntlCalendar::FIELD_DAY_OF_MONTH),
    $cal1->fieldDifference($time, IntlCalendar::FIELD_HOUR_OF_DAY),
    $cal1->fieldDifference($time, IntlCalendar::FIELD_MINUTE)
);
1 year(s), 1 month(s), 1 day(s), 0 hour(s) and 19 minute(s)


echo "Time after:", IntlDateFormatter::formatObject($cal1), "\n";
// After: 9:19:11 am, March 1, 2020
Copy the code

You can see how you can use the fieldDifference() method to get information about calendar objects and comparison dates. Note that after using fieldDifference(), the old calendar objects are all changed to the new date information.

Other information

View the set of locale keyword values

print_r(iterator_to_array(IntlCalendar::getKeywordValuesForLocale('calendar'.'zh_CN'.true)));
// Array
/ / (
// [0] => gregorian
// [1] => chinese
// )
print_r(iterator_to_array(IntlCalendar::getKeywordValuesForLocale('calendar'.'zh_CN'.false)));
// Array
/ / (
// [0] => gregorian
// [1] => chinese
// [2] => japanese
// [3] => buddhist
// [4] => roc
// [5] => persian
// [6] => islamic-civil
// [7] => islamic
// [8] => hebrew
// [9] => indian
// [10] => coptic
// [11] => ethiopic
// [12] => ethiopic-amete-alem
// [13] => iso8601
// [14] => dangi
// [15] => islamic-umalqura
// [16] => islamic-tbla
// [17] => islamic-rgsa
// )
Copy the code

GetKeywordValuesForLocale () method of the first parameter can only be fixed writing calendar, followed by fill in the related area, the content of the return is supported under the current locale information related words.

Regional language type

$cal = IntlCalendar::createInstance(NULL.'@calendar=ethiopic-amete-alem');
var_dump($cal->getType());
// string(19) "ethiopic-amete-alem"

$cal = new IntlGregorianCalendar();
var_dump($cal->getType());
// string(9) "gregorian"
Copy the code

The getType() method obviously returns the type of information for the specified locale.

Scroll to the calendar

var_dump(IntlDateFormatter::formatObject($cal)); // string(31) "November 18, 2020 9:14:59 am"
$cal->roll(IntlCalendar::FIELD_DAY_OF_MONTH, true);
var_dump(IntlDateFormatter::formatObject($cal)); // string(31) "November 19, 2020 9:14:59 am"
Copy the code

Use the roll() method to scroll, or roll, the calendar. In this case, we roll the calendar a day, adding a day.

Convert to a DateTime object

var_dump($cal->toDateTime());
// object(DateTime)#4 (3) {
// ["date"]=>
/ / string (26) "the 2020-11-19 09:14:59. 000000"
// ["timezone_type"]=>
// int(3)
// ["timezone"]=>
// string(13) "Asia/Shanghai"
/ /}
Copy the code

The current IntlCalendar object can be converted to a DateTime object using the toDateTime() method.

Information about all regions supported by the current system

print_r(IntlCalendar::getAvailableLocales());
// Array
/ / (
// [0] => af
// [1] => af_NA
// [2] => af_ZA
// [3] => agq
// [4] => agq_CM
// [5] => ak
// [6] => ak_GH
// [7] => am
// [8] => am_ET
// [9] => ar
/ /...
/ /...
Copy the code

GetAvailableLocales () returns information about all supported locales in the current system.

conclusion

In fact, there are many methods and functions about the calendar class, but people are very dizzy, English explanation is not much, the information is not clear, so here is a simple list of some content. We still respond with the mentality of learning to understand, when the need to use when you can quickly think of these functions can be.

Test code: github.com/zhangyue050…

Reference Documents:

www.php.net/manual/zh/c…

Follow the public account: [Hardcore project manager] to get the latest articles

Add WeChat/QQ friends: free xiaoyuezigonggong / 149844827 】 【 PHP, project management, learning materials

Zhihu, Public Account, Douyin, Toutiao Search [Hardcore Project Manager]

ID of station B: 482780532