2

I've used DateTime::createFromFormat() to parse string into date before, but today I found a problem and I get null from this call.

echo $date_string;
$test = DateTime::createFromFormat('M d Y, H:i:s T', trim($date_string));
echo $test->format('Y-m-d H:i:s');

Output:

Oct 16 2013, 15:45:02 CEST

( ! ) Fatal error: Call to a member function format() on a non-object in (...) on line 51

Where line 51 is the last line where I call format().

So, am I missing something? Thanks in advance!


The output of DateTime::getLastErrors() as requested:

(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 4
    [errors] => Array
        (
            [3] => Unexpected data found.
            [11] => Unexpected data found.
            [22] => Unexpected data found.
            [36] => The timezone could not be found in the database
        )

)

The catch, requested by @whizzzkid:

DateTime::__construct(): Failed to parse time string (Oct 16 2013, 10:00:00 CEST) at position 3 (&): Unexpected character
7
  • Catch error, see what it says. Commented Oct 12, 2013 at 20:03
  • @DevlshOne He echoed $date_string. Commented Oct 12, 2013 at 20:04
  • Can't see anything wrong with this - you must of passed it a string that it wasn't expecting so $test has become false on failure. Commented Oct 12, 2013 at 20:04
  • Maybe your system's timezone table isn't complete, so it doesn't recognize the zone CEST. Commented Oct 12, 2013 at 20:08
  • Hmmm... I get 2013-10-16 15:45:02. Don't see a problem. Commented Oct 12, 2013 at 20:08

4 Answers 4

3

Your string contains UTF non-breaking spaces instead of normal ones. Date parser is unaware of them and does not treat them as whitespace. Replace non-breaking spaces with normal ones with something like:

$date_string = preg_replace('~\x{00a0}~u', ' ', $date_string);
Sign up to request clarification or add additional context in comments.

2 Comments

Well, that is indeed the problem. Thanks!
Very helpful! When scraping sites look at the source when something doesn't work as expected. Ran across something very similar and this is a much easier and cleaner approach than I originally came up with.
2

can you please try this:

<?php
//some $date_string exists
try {
    $test = new DateTime(trim(html_entity_decode($date_string)));
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $test->format('Y-m-d H:i:s');
?>

Try this one!... should work

1 Comment

Yes sir. Output: DateTime::__construct(): Failed to parse time string (Oct&nbsp;16&nbsp;2013,&nbsp;10:00:00&nbsp;CEST) at position 3 (&): Unexpected character
2

I recently had the same problem in my CI project. I solved it with this method, it works:

Instead of this:

$test = new DateTime($date_string);

or

$date_string = preg_replace('~\x{00a0}~u', ' ', $date_string);

as suggested by @Leo Nyx,

I solved it with this:

$date = new DateTime(strtotime($date_string));

and then below it, format the time as you please:

$date->format('d/m/Y');

Off course I already tried solutions posted here. The pre_replace method works, but I think the method I posted here saves you some CPU for good.

The main reason to do this is: somehow your date string contains some characters which DateTime method don't know! strtotime() method correctly converts your string to the date format.

Comments

0

I found an alternate cause to this problem:

I had my date formated with 24 hour time ("H") and am/pm ("a") which confused the parser. So I changed my date format from this: "Y-m-d H:ia T" to "Y-m-d h:ia T" (note the "h") and it all worked correctly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.