Open In App

PHP | IntlDateFormatter formatObject() Function

The IntlDateFormatter::formatObject() function is an inbuilt function in PHP which is used to formats an IntlDateFormatter object. This function allows to format the IntlCalendar or DateTime object. 

Syntax:



string IntlDateFormatter::formatObject( object $object, 
                         mixed $format = NULL, string $locale = NULL )
string datefmt_format_object( object $object,
                         mixed $format = NULL, string $locale = NULL )

Parameters: This function accepts three parameters as mentioned above and described below:

Return Value: This function returns a string in given format on success or False on failure. 



Below program illustrates the IntlDateFormatter::formatObject() function in PHP: 

Program: 




<?php
 
// Set the timezone and locale
ini_set('date.timezone', 'Asia/Calcutta');
ini_set('intl.default_locale', 'en_US');
 
// Create an IntlCalendar from a DateTime object or string
$calendar = IntlCalendar::fromDateTime('2019-10-05 09:19:29'); 
 
// Display the date in given format
echo "Default date format => " .
    IntlDateFormatter::formatObject($calendar) . "\n";
 
// Display the date in given format
echo "Date in string format => " .
    IntlDateFormatter::formatObject($calendar,
    "dd MM yyyy") . "\n";
 
// Display the date in given format
echo "Date in long format => " .
    IntlDateFormatter::formatObject($calendar,
    IntlDateFormatter::TRADITIONAL) . "\n";
 
// Display the date in given format
echo "Date in array format => ",
    IntlDateFormatter::formatObject($calendar,
    array(
        IntlDateFormatter::NONE,
        IntlDateFormatter::FULL)
    );
 
?>

Output:
Default date format => Oct 5, 2019, 9:19:29 AM
Date in string format => 05 10 2019
Date in long format => Saturday, October 5, 2019 at 9:19:29 AM India Standard Time
Date in array format => 9:19:29 AM India Standard Time

Reference: https://www.php.net/manual/en/intldateformatter.formatobject.php

Article Tags :