Open In App

PHP | IntlDateFormatter format() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

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


Last Updated : 10 Oct, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads