|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-12-20 12:00 UTC] lesichkovm at gmail dot com
Description:
------------
String to time does not work correct when a year is provided.
Example:
echo date('Y-m-d', strtotime('2016'));
Returns:
2019-12-20
Test script:
---------------
<?php
echo date('Y-m-d', strtotime('2016'));
Expected result:
----------------
2016-01-01
Actual result:
--------------
2019-12-20
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 08 20:00:02 2025 UTC |
The string "2016" is interpreted as a two-digit hour and two-digit minutes time, rather than a four-digit year. To demonstrate this: echo date('H:i', strtotime('2016')); // 20:16 This case is explicitly mentioned in the "Date Formats" manual page [1]: > Note: The "Year (and just the year)" format only works if a time > string has already been found -- otherwise this format is > recognised as HH MM. [1] https://www.php.net/manual/en/datetime.formats.date.phpThis definitely does not make sense from any perspective. I have not seen anyone use hour:minute time format without : as in 20:19. But literally everyone uses 2019 as represenation of years. There is no other use case for 4 numbers but to be a year. This should be revisited. Now to get the correct year I have to do somersaults like: if(strlen($year)==4){ $year = $year.'-01-01'; } echo date('Y-m-d', strtotime($year)); I don't see how this benefits anyone