php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64975 createFromFormat Error parsing when AM PM not at the end
Submitted: 2013-06-05 11:16 UTC Modified: 2021-08-18 19:33 UTC
Votes:11
Avg. Score:4.0 ± 0.9
Reproduced:10 of 10 (100.0%)
Same Version:7 (70.0%)
Same OS:5 (50.0%)
From: dimka212 at gmail dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: Irrelevant OS: Any
Private report: No CVE-ID: None
 [2013-06-05 11:16 UTC] dimka212 at gmail dot com
Description:
------------
$date = DateTime::createFromFormat('d M Y A h:i', '11 Mar 2013 PM 3:34');
Returns:   [12] => Unexpected data found.

Works only in this case:
$date = DateTime::createFromFormat('d M Y h:i A', '11 Mar 2013 3:34 PM');


Test script:
---------------
$date = DateTime::createFromFormat('d M Y A h:i', '11 Mar 2013 PM 3:34');
if(!$date) echo 'Doesn\'t work =(';

Expected result:
----------------
Returns DateTime object

Actual result:
--------------
False
(Unexpected data found.)

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-09 12:20 UTC] daverandom@php.net
This behaviour is not in itself a bug.

The issue is that the wrong error is reported, and the root cause of this is actually a bug in the way date_get_last_errors() works rather than the date parsing mechanism.

As fixing this would be a BC break, I have submitted a patch that ensures that in this specific case the correct error is reported.
 [2014-05-09 20:38 UTC] daverandom@php.net
-Status: Open +Status: Analyzed -Operating System: Windows +Operating System: Any
 [2014-06-08 21:35 UTC] stas@php.net
-Assigned To: +Assigned To: derick
 [2016-04-22 05:18 UTC] miken32 at gmail dot com
If the behaviour is "not a bug" then documentation should also be updated to mention that AM/PM will not be parsed until after an hour is parsed.

I fail to see why this limitation is in any way necessary; some explanation would be helpful!
 [2017-10-24 05:24 UTC] kalle@php.net
-Status: Analyzed +Status: Assigned
 [2021-08-18 19:33 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2021-08-18 19:33 UTC] derick@php.net
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from https://github.com/php/php-src and re-test.
Thank you for the report, and for helping us make PHP better.

This is fixed for PHP 8.0(.10) and later, where it now correctly shows the error message:

<?php
$date = DateTime::createFromFormat('d M Y A h:i', '11 Mar 2013 PM 3:34');
var_dump(DateTime::getLastErrors());
?>

array(4) {
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(1)
  ["errors"]=>
  array(1) {
    [12]=>
    string(51) "Meridian can only come after an hour has been found"
  }
}
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 06:01:30 2024 UTC