Skip to content
Related Articles

Related Articles

PHP | IntlDateFormatter getErrorCode() Function,
  • Last Updated : 10 Oct, 2019

The IntlDateFormatter::getErrorCode() function is an inbuilt function in PHP which is used to return the error code from last operation.

Syntax:

  • Object-oriented style:
    int IntlDateFormatter::getErrorCode( void )
  • Procedural style:
    int datefmt_get_error_code( IntlDateFormatter $fmt )

Parameters: This function uses a single parameter $fmt which holds the resource of formatter.

Return Value: This function returns the error code, one of UErrorCode values. Initial value is U_ZERO_ERROR.

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



Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Create a date formatter
$formatter = datefmt_create(
    'en_US',
    IntlDateFormatter::SHORT,
    IntlDateFormatter::SHORT,
    'Asia/Kolkata',
    IntlDateFormatter::GREGORIAN
);
  
// Format the date/time value
// as a string
$str = datefmt_format($formatter);
  
if (!$str) {
    echo "Error code: "
        datefmt_get_error_code($formatter) . "\n";
      
    echo "Error message: "
        datefmt_get_error_message($formatter);
}
  
echo "\n\n";
  
// Format the date/time value
// as a string
$str = $formatter->format("geeks");
  
if (!$str) {
    echo "Error code: "
        $formatter->getErrorCode() . "\n";
      
    echo "Error message: "
        $formatter->getErrorMessage();
}
  
?>

chevron_right


Error:

PHP Warning:  datefmt_format() expects exactly 2 parameters, 1 given
in /home/700d8660f05cec95beb6e1ab21252ab1.php on line 14

Output:

Error code: 0
Error message: U_ZERO_ERROR

Error code: 1
Error message: datefmt_format: string 'geeks' is not numeric, which would
be required for it to be a valid date: U_ILLEGAL_ARGUMENT_ERROR

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :