|  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
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
$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:
(Unexpected data found.)


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2014-05-09 12:20 UTC]
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]
-Status: Open +Status: Analyzed -Operating System: Windows +Operating System: Any
 [2014-06-08 21:35 UTC]
-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]
-Status: Analyzed +Status: Assigned
 [2021-08-18 19:33 UTC]
-Status: Assigned +Status: Closed
 [2021-08-18 19:33 UTC]
The fix for this bug has been committed.
If you are still experiencing this bug, try to check out latest source from 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:

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

array(4) {
  array(0) {
  array(1) {
    string(51) "Meridian can only come after an hour has been found"
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Mon Apr 15 14:01:33 2024 UTC