(PHP 5 >= 5.5.0, PHP 7, PHP 8, PECL >= 3.0.0a1)
IntlCalendar::fieldDifference — 計算給定時間與此物件時間之間的差異
物件導向風格
程序式風格
傳回指定時間與此物件設定時間的差值,差值單位由 field
參數指定。
此方法設計為連續呼叫,先從最重要的欄位開始,依序到最不重要的欄位。為此,此日曆中指定欄位的值會隨著傳回值而增加,作為副作用。
calendar
一個 IntlCalendar 實例。
timestamp
用於比較 field
所代表數量的時間。若要結果為正值,此參數指定的時間必須晚於呼叫此方法之物件的時間。
field
表示所比較數量的欄位。
IntlCalendar 日期/時間 欄位常數 之一。這些是介於 0
和 IntlCalendar::FIELD_COUNT
之間的整數值。
傳回指定欄位相關單位下的時間差值(有正負號),或是在失敗時傳回 false
。
範例 #1 IntlCalendar::fieldDifference()
<?php
ini_set('date.timezone', 'Europe/Lisbon');
ini_set('intl.default_locale', 'fr_FR');
$cal1 = IntlCalendar::fromDateTime('2012-02-29 09:00:11');
$cal2 = IntlCalendar::fromDateTime('2013-03-01 09:19:29');
$time = $cal2->getTime();
echo "Time before: ", IntlDateFormatter::formatObject($cal1), "\n";
printf(
"The difference in time is %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)
);
//現在時間已推進到目標時間,但秒數例外,
//因為我們沒有計算秒數的差異
echo "Time after: ", IntlDateFormatter::formatObject($cal1), "\n";
上述範例將輸出:
Time before: 29 févr. 2012 09:00:11 The difference in time is 1 year(s), 0 month(s), 1 day(s), 0 hour(s) and 19 minute(s) Time after: 1 mars 2013 09:19:11