php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79006 Function strtotime Misbehaves with Years
Submitted: 2019-12-20 12:00 UTC Modified: 2019-12-21 06:56 UTC
From: lesichkovm at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.4.1 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: lesichkovm at gmail dot com
New email:
PHP Version: OS:

 

 [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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-20 12:15 UTC] salathe@php.net
-Status: Open +Status: Not a bug
 [2019-12-20 12:15 UTC] salathe@php.net
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.php
 [2019-12-21 06:56 UTC] lesichkovm at gmail dot com
This 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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 03:01:27 2024 UTC