English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Grundanleitung

PHP Fortgeschrittene Anleitung

PHP & MySQL

PHP Referenzhandbuch

PHP date_get_last_errors() Funktionssatz und Beispiel

PHP Date & Time Funktionshandbuch

date_get_last_errors() Funktion holt Warnungen und Fehlerinformationen

Definition und Verwendung

date_get_last_errors() ist ein Alias für DateTime::getLastErrors()::__construct(). Diese Funktion dient dazu, Warnungen und Fehler zu erhalten, die bei der Analyse von Datumsschnipseln auftreten.

Syntax

date_get_last_errors();

Parameters

This function does not accept any parameters

Return value

The PHP date_get_last_errors() function returns an array containing all warnings and errors that occur when you try to parse a date string.

PHP version

This function was originally introduced in PHP version5.5introduced in version 5.3.0 and can be used in all higher versions.

Online example

The following example demonstratesdate_get_last_errors()Function usage-

<?php
   date_create("215-7896-848");
   $errors = date_get_last_errors();
   print_r($errors);
?>
Test to see‹/›

Output result

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [8] => Double timezone specification
        )
    [error_count] => 5
    [errors] => Array
        (
            [0] => Unexpected character
            [1] => Unexpected character
            [2] => Unexpected character
            [6] => Unexpected character
            [7] => Unexpected character
        )
)

Online example

Using this function, you can capture errors that occur when creating a date, as shown below-

<?php
   try { 
      $res = new DateTime("215-7896-848");
      print($res);
   } catch (Exception $e) { 
      print_r(DateTime::getLastErrors()); 
   }  
?>
Test to see‹/›

Output result

Array
(
    [warning_count] => 1
    [warnings] => Array
        (
            [8] => Double timezone specification
        )
    [error_count] => 5
    [errors] => Array
        (
            [0] => Unexpected character
            [1] => Unexpected character
            [2] => Unexpected character
            [6] => Unexpected character
            [7] => Unexpected character
        )
)

Online example

The following example shows the use ofdate_create_from_format()Error occurred when creating DateTime object/Warning-

//Create a DateTime object
$date = "25-Mar-1989";
$format = "d-Z-Y";
$res = date_create_from_format($format, $date);
print_r(date_get_last_errors());
Test to see‹/›

Output result

Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )
    [error_count] => 3
    [errors] => Array
        (
            [3] => The format separator does not match
            [4] => Unexpected data found.
        )
)