Supported Date and Time Formats

Table of Contents

This section describes all the different formats that the strtotime(), DateTime and date_create() parser understands. The formats are grouped by section. In most cases formats from different sections can be used in the same date/time string. For each of the supported formats, one or more examples are given, as well as a description for the format. Characters in single quotes in the formats are case-insensitive ('t' could be t or T), characters in double quotes are case-sensitive ("T" is only T).

Коментарии

Автор:
The year-only format is a potential issue that could cause problems for you.

<?php
echo (new DateTime('1994'))->format('Y-m-d');
// Outputs today's date in the year 1984, e.g. "1994-08-15"
echo (new DateTime('2004'))->format('Y-m-d');
// Outputs today's date in the *current* year, e.g. "2024-08-15"
?>

This is because, as noted above in the documentation, 2004 gets interpreted as a time, e.g. 20:04, rather than as a year. Even though 1994 is interpreted as a year.

One solution is to add ' T' to the end of the string:

<?php
echo (new DateTime('1994 T'))->format('Y-m-d');
// "1994-08-15"
echo (new DateTime('2004 T'))->format('Y-m-d');
// "2004-08-15"
?>

Another would be to add an arbitrary time to the beginning of the string:

<?php
echo (new DateTime('11:00 1994'))->format('Y-m-d');
// "1994-08-15"
echo (new DateTime('11:00 2004'))->format('Y-m-d');
// "2004-08-15"
?>
2024-08-15 23:12:31
http://php5.kiev.ua/manual/ru/datetime.formats.html
Автор:
I wrote previously that appending a 'T' to a year forces PHP to interpret the date as a year rather than a time. This does not appear to be universally true. While this method did work for me in my previous development environment (PHP 8.1 on Ubuntu 20.04), it does not work after upgrading my development environment (PHP 8.3 on Ubuntu 24.04). The 'T' is instead seen as a timezone.

Further testing shows that PHP 8.1 (and even earlier PHP versions) also have this new behavior I am observing. Therefore, I can only assume the behavior is dependent on environment and/or build rather than PHP version. 

The suggestion about adding an actual time string to the year, e.g. '00:00 2004', is still valid. This appears to be the only reliable way to force PHP to interpret a string as a date rather than a datetime.
2024-09-23 18:11:50
http://php5.kiev.ua/manual/ru/datetime.formats.html

    Поддержать сайт на родительском проекте КГБ