查詢

IntlDateFormatter::setCalendar()函式—用法及示例

「 設定IntlDateFormatter物件的日曆型別 」


函式名稱:IntlDateFormatter::setCalendar()

適用版本:PHP 5 >= 5.5.0, PHP 7, PHP 8

函式描述:設定IntlDateFormatter物件的日曆型別。

用法:

bool IntlDateFormatter::setCalendar ( mixed $calendar )

引數:

  • $calendar:要設定的日曆型別。可以是以下常量之一:
    • IntlDateFormatter::TRADITIONAL:傳統的日曆型別。
    • IntlDateFormatter::GREGORIAN:公曆型別。

返回值:

  • 成功時返回 true,失敗時返回 false

示例:

$formatter = new IntlDateFormatter('en_US', IntlDateFormatter::SHORT, IntlDateFormatter::NONE);

// 設定日曆型別為傳統
$formatter->setCalendar(IntlDateFormatter::TRADITIONAL);
echo $formatter->format(time());  // 輸出:12/31/21

// 設定日曆型別為公曆
$formatter->setCalendar(IntlDateFormatter::GREGORIAN);
echo $formatter->format(time());  // 輸出:12/31/2021

以上示例建立了一個IntlDateFormatter物件,並透過setCalendar()方法設定了日曆型別。首先,設定日曆型別為傳統(IntlDateFormatter::TRADITIONAL),然後使用format()方法將當前時間格式化為短日期字串。接著,將日曆型別設定為公曆(IntlDateFormatter::GREGORIAN),再次使用format()方法將當前時間格式化為短日期字串。最後,分別輸出了兩個不同日曆型別下的日期格式化結果。

補充糾錯
熱門PHP函式
分享連結