Saturday, September 6, 2025
HomeLanguagesPHP | IntlCalendar fieldDifference() Function

PHP | IntlCalendar fieldDifference() Function

The IntlCalendar::fieldDifference() function is an inbuilt function in PHP which is used to return the difference between given time and the object time.

Syntax:

  • Object oriented style:
    int IntlCalendar::fieldDifference( float $when, int $field )
  • Procedural style:
    int intlcal_field_difference( IntlCalendar $cal, float $when, int $field )

Parameters:

  • $cal: This parameter holds the IntlCalendar resource name.
  • $when: This parameter holds the time against which to compare the quantity represented by the field.
  • $field: This parameter holds the field that represents the quantity being compared.

Return Value: This function returns a signed difference of time in the unit associated with the specified field on success or FALSE on failure.

Below program illustrates the IntlCalendar::fieldDifference() function in PHP:

Program:




<?php
  
// Set the timezone
ini_set('date.timezone', 'Asia/Calcutta');
  
// Get the time from calendar
$calendar1 = IntlCalendar::fromDateTime('2018-12-21 09:30:25');
$calendar2 = IntlCalendar::fromDateTime('2019-08-29 11:20:20');
  
// Get time represented by the object
$calTime = $calendar2->getTime();
  
// Display the first time
echo "First Time: " . IntlDateFormatter::formatObject($calendar1) . "\n";
  
// Display the last time
echo "Last Time: " . IntlDateFormatter::formatObject($calendar2) . "\n";
  
// Time difference
echo "Time difference: "
    . $calendar1->fieldDifference($calTime, 
            IntlCalendar::FIELD_YEAR) . "-Years "
      
    . $calendar1->fieldDifference($calTime,
            IntlCalendar::FIELD_MONTH) . "-Months "
      
    . $calendar1->fieldDifference($calTime, 
            IntlCalendar::FIELD_DAY_OF_MONTH) . "-Days "
      
    . $calendar1->fieldDifference($calTime, 
            IntlCalendar::FIELD_HOUR_OF_DAY) . "-Hours "
      
    . $calendar1->fieldDifference($calTime, 
            IntlCalendar::FIELD_MINUTE) . "-Minutes";
  
?>


Output:

First Time: Dec 21, 2018, 9:30:25 AM
Last Time: Aug 29, 2019, 11:20:20 AM
Time difference: 0-Years 8-Months 8-Days 1-Hours 49-Minutes

Reference: https://www.php.net/manual/en/intlcalendar.fielddifference.php

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6641 POSTS0 COMMENTS
Nicole Veronica
11806 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11869 POSTS0 COMMENTS
Shaida Kate Naidoo
6754 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS