PHP.nl

date_parse

date_parse

Returns associative array with detailed info about given date/time

array **date_parse** string $datetime
parses the given
string according to the same rules as
and

. Instead of returning a Unix timestamp (with ) or a object (with ), it returns an associative array with the information that it could detect in the given string. date_parse``datetime``strtotime``DateTimeImmutable::__construct``strtotime``DateTimeImmutable``DateTimeImmutable::__construct``datetime

If no information about a certain group of elements can be found, these array elements will be set to false or are missing. If needed for constructing a timestamp or object from the same string, more fields can be set to a non-false value. See the examples for cases where that happens. DateTimeImmutable``datetime

datetime Date/time in format accepted by . DateTimeImmutable::__construct

Returns with information about the parsed date/time. array

The returned array has keys for , , , , , , , and . year``month``day``hour``minute``second``fraction``is_localtime

If is present then indicates the type of timezone. For type (UTC offset) the , fields are added; for type (abbreviation) the fields , are added; and for type (timezone identifier) the , are added. is_localtime``zone_type``1``zone``is_dst``2``tz_abbr``is_dst``3``tz_abbr``tz_id

If relative time elements are present in the string such as , the then returned array includes a nested array with the key . This array then contains the keys , , , , , , and if necessary , and , depending on the string that was passed in. datetime``+3 days``relative``year``month``day``hour``minute``second``weekday``weekdays

The array includes and fields. The first one indicate how many warnings there were. The keys of elements array indicate the position in the given where the warning occurred, with the string value describing the warning itself. warning_count``warnings``warnings``datetime

The array also contains and fields. The first one indicate how many errors were found. The keys of elements array indicate the position in the given where the error occurred, with the string value describing the error itself. error_count``errors``errors``datetime

Waarschuwing: > The number of array elements in the and arrays might be less than or if they occurred at the same position. warnings``errors``warning_count``error_count

In case the date/time format has an error, the element 'errors' will contain the error messages.

Voorbeeld: A example with a comprehensive string

<?php
var_dump(date_parse("2006-12-12 10:00:00.5"));
array(12) {
  ["year"]=>
  int(2006)
  ["month"]=>
  int(12)
  ["day"]=>
  int(12)
  ["hour"]=>
  int(10)
  ["minute"]=>
  int(0)
  ["second"]=>
  int(0)
  ["fraction"]=>
  float(0.5)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(false)
}

The timezone elements only show up if they are included in the given string. In that case there will always be a element and a few more depending on its value.

datetime``zone_typeVoorbeeld: with timezone abbreviation information

<?php
var_dump(date_parse("June 2nd, 2022, 10:28:17 BST"));
array(16) {
  ["year"]=>
  int(2022)
  ["month"]=>
  int(6)
  ["day"]=>
  int(2)
  ["hour"]=>
  int(10)
  ["minute"]=>
  int(28)
  ["second"]=>
  int(17)
  ["fraction"]=>
  float(0)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(true)
  ["zone_type"]=>
  int(2)
  ["zone"]=>
  int(0)
  ["is_dst"]=>
  bool(true)
  ["tz_abbr"]=>
  string(3) "BST"
}

Voorbeeld: with timezone identifier information

<?php
var_dump(date_parse("June 2nd, 2022, 10:28:17 Europe/London"));
array(14) {
  ["year"]=>
  int(2022)
  ["month"]=>
  int(6)
  ["day"]=>
  int(2)
  ["hour"]=>
  int(10)
  ["minute"]=>
  int(28)
  ["second"]=>
  int(17)
  ["fraction"]=>
  float(0)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(true)
  ["zone_type"]=>
  int(3)
  ["tz_id"]=>
  string(13) "Europe/London"
}

If a more minimal string is parsed, less information is available. In this example, all the time parts are returned as false.

datetimeVoorbeeld: with a minimal string

<?php
var_dump(date_parse("June 2nd, 2022"));
array(12) {
  ["year"]=>
  int(2022)
  ["month"]=>
  int(6)
  ["day"]=>
  int(2)
  ["hour"]=>
  bool(false)
  ["minute"]=>
  bool(false)
  ["second"]=>
  bool(false)
  ["fraction"]=>
  bool(false)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(false)
}
do not

influence the values parsed from absolute formats, but are parsed into the "relative" element.

Relative formatsVoorbeeld: with relative formats

<?php
var_dump(date_parse("2006-12-12 10:00:00.5 +1 week +1 hour"));
array(13) {
  ["year"]=>
  int(2006)
  ["month"]=>
  int(12)
  ["day"]=>
  int(12)
  ["hour"]=>
  int(10)
  ["minute"]=>
  int(0)
  ["second"]=>
  int(0)
  ["fraction"]=>
  float(0.5)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(false)
  ["relative"]=>
  array(6) {
    ["year"]=>
    int(0)
    ["month"]=>
    int(0)
    ["day"]=>
    int(7)
    ["hour"]=>
    int(1)
    ["minute"]=>
    int(0)
    ["second"]=>
    int(0)
  }
}

Some stanzas, such as will set the time portion of the string to . If is passed to it would also have resulted in the hour, minute, second, and fraction being set to . In the example below, the year element is however left as false.

Thursday``0``Thursday``DateTimeImmutable::__construct``0Voorbeeld: with side-effects

<?php
var_dump(date_parse("Thursday, June 2nd"));
array(13) {
  ["year"]=>
  bool(false)
  ["month"]=>
  int(6)
  ["day"]=>
  int(2)
  ["hour"]=>
  int(0)
  ["minute"]=>
  int(0)
  ["second"]=>
  int(0)
  ["fraction"]=>
  float(0)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(false)
  ["relative"]=>
  array(7) {
    ["year"]=>
    int(0)
    ["month"]=>
    int(0)
    ["day"]=>
    int(0)
    ["hour"]=>
    int(0)
    ["minute"]=>
    int(0)
    ["second"]=>
    int(0)
    ["weekday"]=>
    int(4)
  }
}

date_parse_from_format``datetime``checkdate``getdate