Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime::createFromFormat not working as expected

Tags:

php

datetime

Running PHP 5.3.28

I have a particular date getting returned as a string that strtotime is choking on, so I thought to try out DateTime::createFromFormat(), however despite best efforts getting errors.

<?php

$freshdate = '09/07/2015 (Mon)';
$date = DateTime::createFromFormat("m/d/Y (D)", $freshdate);
if (!$date) {
  var_dump('error', DateTime::getLastErrors());
}

Result is:

array (size=4)
  'warning_count' => int 0
  'warnings' => array (size=0)
  'error_count' => int 2
  'errors' => array (size=2)
      12 => string 'A textual day could not be found' (length=32)
      16 => string 'Data missing' (length=12)
like image 328
Mr Griever Avatar asked Dec 19 '25 04:12

Mr Griever


1 Answers

This is bug in PHP#65554:

createFromFormat fails when in the format D or l is followed by separators that are not space or comma.

How to fix? Change your format or upgrade your PHP version.

like image 144
Andrzej Budzanowski Avatar answered Dec 20 '25 17:12

Andrzej Budzanowski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!