php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79387 createFromFormat works wrong with incorrect dates
Submitted: 2020-03-16 12:32 UTC Modified: 2020-03-16 12:53 UTC
From: amriwman at gmail dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.4.3 OS: linux
Private report: No CVE-ID: None
 [2020-03-16 12:32 UTC] amriwman at gmail dot com
Description:
------------
Incorrect creation from incorrect dates with  DateTimeImmutable::createFromFormat


Test script:
---------------
        try{
            $date = '2021-31-31';
        
            $date1 = \DateTimeImmutable::createFromFormat('Y-m-d', $date);
            var_dump($date1);
        
            $date2 = new \DateTimeImmutable($date);
        }catch (\Throwable $e){
            var_dump($e->getMessage());
        }
        die;

Expected result:
----------------
Failed to parse time string (2021-31-31) at position 6 (6): Unexpected character

Actual result:
--------------
php version 7.4.2

class DateTimeImmutable#905 (0) {
}


sometimes it's even such:

DateTimeImmutable @1690803734 {#905
  date: 2023-07-31 11:42:14.0 UTC (+00:00)
}

php version 7.4.3

object(DateTimeImmutable)#985 (3) {
  ["date"]=>
  string(26) "2023-07-31 14:26:58.000000"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(15) "Europe/Helsinki"
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-16 12:53 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2020-03-16 12:53 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Normally, PHP allows values that are out of range with DateTime, such as with:

$date2 = new \DateTimeImmutable("2020-02-31");

In order to check whether the parsed date was valid you can then use:

var_dump(DateTime::getLastErrors());

This behaviour is for historical reasons, and we can't now change that.


This extends to DateTimeImmutable::createFromFormat() as well. As long as the data *format* is correct, the method will not throw. Again, you can use getLastErrors():

derick@singlemalt:/tmp$ cat 79387.php
<?php
$date = '2021-31-31';

$date1 = \DateTimeImmutable::createFromFormat('Y-m-d', $date);
var_dump($date1);
var_dump(DateTime::getLastErrors());

derick@singlemalt:/tmp$ php 79387.php
/tmp/79387.php:5:
class DateTimeImmutable#1 (3) {
  public $date =>
  string(26) "2023-07-31 21:52:17.000000"
  public $timezone_type =>
  int(3)
  public $timezone =>
  string(10) "Asia/Tokyo"
}
/tmp/79387.php:6:
array(4) {
  'warning_count' =>
  int(1)
  'warnings' =>
  array(1) {
    [10] =>
    string(27) "The parsed date was invalid"
  }
  'error_count' =>
  int(0)
  'errors' =>
  array(0) {
  }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 18:01:34 2024 UTC