Open In App

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:



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

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

Article Tags :