date_parse

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

date_parseReturns associative array with detailed info about given date/time

Description

date_parse(string $datetime): array

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

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 DateTimeImmutable object from the same datetime string, more fields can be set to a non-false value. See the examples for cases where that happens.

Parameters

datetime

Date/time in format accepted by DateTimeImmutable::__construct().

Return Values

Returns array with information about the parsed date/time on success or false on failure.

Errors/Exceptions

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

Changelog

Version Description
7.2.0 The zone element of the returned array represents seconds instead of minutes now, and its sign is inverted. For instance -120 is now 7200.

Examples

Example #1 A date_parse() example with a comprehensive datetime string

<?php
var_dump
(date_parse("2006-12-12 10:00:00.5"));
?>

The above example will output:

array(12) {
  'year' => int(2006)
  'month' => int(12)
  'day' => int(12)
  'hour' => int(10)
  'minute' => int(0)
  'second' => int(0)
  'fraction' => double(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 datetime string. In that case there will always be a zone_type element and a few more depending on its value.

Example #2 date_parse() with timezone abbreviation information

<?php
var_dump
(date_parse("June 2nd, 2022, 10:28:17 BST"));
?>

The above example will output:

array(16) {
  'year' => int(2022)
  'month' => int(6)
  'day' => int(2)
  'hour' => int(10)
  'minute' => int(28)
  'second' => int(17)
  'fraction' => double(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"
}

Example #3 date_parse() with timezone identifier information

<?php
var_dump
(date_parse("June 2nd, 2022, 10:28:17 Europe/London"));
?>

The above example will output:

array(14) {
  'year' => int(2022)
  'month' => int(6)
  'day' => int(2)
  'hour' => int(10)
  'minute' => int(28)
  'second' => int(17)
  'fraction' => double(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 datetime string is parsed, less information is available. In this example, all the time parts are returned as false.

Example #4 date_parse() with a minimal string

<?php
var_dump
(date_parse("June 2nd, 2022"));
?>

The above example will output:

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)
}

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

Example #5 date_parse() with relative formats

<?php
var_dump
(date_parse("2006-12-12 10:00:00.5 +1 week +1 hour"));
?>

The above example will output:

array(13) {
  'year' => int(2006)
  'month' => int(12)
  'day' => int(12)
  'hour' => int(10)
  'minute' => int(0)
  'second' => int(0)
  'fraction' => double(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 Thursday will set the time portion of the string to 0. If Thursday is passed to DateTimeImmutable::__construct() it would also have resulted in the hour, minute, second, and fraction being set to 0. In the example below, the year element is however left as false.

Example #6 date_parse() with side-effects

<?php
var_dump
(date_parse("Thursday, June 2nd"));
?>

The above example will output:

array(13) {
  'year' => bool(false)
  'month' => int(6)
  'day' => int(2)
  'hour' => int(0)
  'minute' => int(0)
  'second' => int(0)
  'fraction' => double(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)
  }
}

See Also

  • date_parse_from_format() - Get info about given date formatted according to the specified format for parsing a datetime with a specific given format
  • checkdate() - Validate a Gregorian date
  • getdate() - Get date/time information

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial