Friday, September 5, 2025
HomeLanguagesPHP | IntlDateFormatter format() Function

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:




<?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);
  
?>


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

RELATED ARTICLES

Most Popular

Dominic
32267 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6635 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11865 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7026 POSTS0 COMMENTS
Thapelo Manthata
6703 POSTS0 COMMENTS
Umr Jansen
6720 POSTS0 COMMENTS