PHP | IntlDateFormatter format() Function

The IntlDateFormatter::format() function is an inbuilt function in PHP which is used to format the date/time value as a string.

Syntax:

  • Object oriented style:
    string IntlDateFormatter::format( mixed $value )
  • Procedural style:
    string datefmt_format( IntlDateFormatter $fmt, mixed $value )

Parameters: This function uses two parameters as mentioned above and described below:



  • fmt: This parameter holds the resource of date object.
  • value: This parameter holds the value to the format. It may be a DateTimeInterface object, an IntlCalendar object, or a numeric type representing a number of seconds. If DateTime or IntlCalendar object is passed then it is not considered.

Return Value: This function returns the formatted string on success or False when error occurred.

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

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a date formatter
$fmt = datefmt_create(
    'en_US',
    IntlDateFormatter::LONG,
    IntlDateFormatter::LONG,
    'Asia/Kolkata',
    IntlDateFormatter::GREGORIAN
);
  
// Display the date in given format
echo 'Formatted output using object oriented style: '
            . $fmt->format(0) . "\n";
  
echo 'Formatted output using procedural style: '
            . datefmt_format($fmt, 0) . "\n\n";
  
// Create a date formatter
$fmt = datefmt_create(
    'en_US',
    IntlDateFormatter::SHORT,
    IntlDateFormatter::SHORT,
    'Asia/Kolkata',
    IntlDateFormatter::GREGORIAN
);
  
// Display the date in given format
echo 'Formatted output using object oriented style: '
            . $fmt->format(0) ."\n";
              
echo 'Formatted output using procedural style: '
            . datefmt_format($fmt, 0);
  
?>

chevron_right


Output:

Formatted output using object oriented style: January 1, 1970 at 5:30:00 AM GMT+5:30
Formatted output using procedural style: January 1, 1970 at 5:30:00 AM GMT+5:30

Formatted output using object oriented style: 1/1/70, 5:30 AM
Formatted output using procedural style: 1/1/70, 5:30 AM

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.