Michael
Michael

Reputation: 3228

PHP parse string to dateTime() with

I want to parse the following string to a DateTime() Object in PHP:

2017-03-03T09:06:41.187

I try to do this as follows:

$stateCreatedOn = DateTime::createFromFormat('Y-m-dTH:i:s.u','2017-03-03T09:06:41.187');
var_dump($stateCreatedOn); // -> Returns false

However, parsing doesn´t work and the variable is always set to false. Anybody an idea what´s wrong with my date format specification?

Thanks a lot in advance!

Upvotes: 3

Views: 6470

Answers (3)

Vikas Biradargoudar
Vikas Biradargoudar

Reputation: 134

$stateCreatedOn = DateTime::createFromFormat('Y-m-d H:i:s.u','2017-03-03 09:06:41.187'); var_dump($stateCreatedOn);

Result : object(DateTime)#1 (3) { ["date"]=> string(26) "2017-03-03 09:06:41.187000" ["timezone_type"]=> int(3) ["timezone"]=> string(13) "Europe/Berlin" }

Upvotes: 0

ArmKh
ArmKh

Reputation: 445

Use strtotime() function

echo strtotime('2017-03-03T09:06:41.187'); // result will be something like 1488517601

Upvotes: 1

Dan Ionescu
Dan Ionescu

Reputation: 3423

$datetime = DateTime::createFromFormat('Y-m-d\TH:i:s+', '2017-03-03T09:06:41.187');
print_r($datetime);

prints:

DateTime Object
(
    [date] => 2017-03-03 09:06:41.000000
    [timezone_type] => 3
    [timezone] => Europe/Helsinki
)

Upvotes: 7

Related Questions