Bug #79387 [Opn->Nab]: createFromFormat works wrong with incorrect dates

From: Date: Mon, 16 Mar 2020 12:53:08 +0000
Subject: Bug #79387 [Opn->Nab]: createFromFormat works wrong with incorrect dates
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-226124@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79387&edit=1

 ID:                 79387
 Updated by:         derick@php.net
 Reported by:        amriwman at gmail dot com
 Summary:            createFromFormat works wrong with incorrect dates
-Status:             Open
+Status:             Not a bug
 Type:               Bug
 Package:            Date/time related
 Operating System:   linux
 PHP Version:        7.4.3
 Block user comment: N
 Private report:     N

 New Comment:

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


Previous Comments:
------------------------------------------------------------------------
[2020-03-16 12:32:38] 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"
}



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79387&edit=1


Thread (2 messages)

« previous php.bugs (#226124) next »