函式:IntlDateFormatter::getCalendarObject()
適用版本:PHP 5 >= 5.3.0, PHP 7, PHP 8
用法: IntlDateFormatter::getCalendarObject() 方法用於獲取與日期格式化程式關聯的日曆物件。它返回一個 Calendar 物件,您可以使用該物件執行與日期相關的操作,如日期計算、格式化和解析。
語法: public IntlCalendar IntlDateFormatter::getCalendarObject ( void )
引數: 該方法沒有引數。
返回值: 返回一個 IntlCalendar 物件,表示與日期格式化程式關聯的日曆。
示例:
$date = new DateTime('2022-05-15');
$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Los_Angeles', IntlDateFormatter::GREGORIAN);
$calendar = $formatter->getCalendarObject();
// 設定日期為當前日期
$calendar->setTime($date->getTimestamp());
// 獲取日期的星期幾
$weekday = $calendar->get(IntlCalendar::DOW_IN_WEEK);
// 獲取日期的月份
$month = $calendar->get(IntlCalendar::MONTH);
// 獲取日期的年份
$year = $calendar->get(IntlCalendar::YEAR);
echo "Today is " . $calendar->getDisplayName(IntlCalendar::DAY_OF_WEEK, IntlCalendar::LONG) . ", " . $calendar->getDisplayName(IntlCalendar::MONTH, IntlCalendar::LONG) . " " . $calendar->get(IntlCalendar::DAY_OF_MONTH) . ", " . $calendar->get(IntlCalendar::YEAR);
在上面的示例中,我們首先建立了一個 DateTime 物件表示日期,然後建立了一個 IntlDateFormatter 物件來格式化日期。然後,我們使用 getCalendarObject() 方法獲取與日期格式化程式關聯的日曆物件。接下來,我們使用日曆物件執行一些操作,如設定日期和獲取日期的各個部分。最後,我們使用 getDisplayName() 方法獲取星期幾和月份的本地化名稱,並將它們列印出來。
熱門工具排行榜