Nadir Fouka
Nadir Fouka

Reputation: 462

DateTime Parsing Date with PHP

I Want to convert this date " Apr 11 16:21:29.219 2019 GMT" to timestamp in PHP ,

This is my code :

$date = \DateTime::createFromFormat('M d H:i:s.u Y' , 
date_format(new \DateTime("Apr 11 16:21:29.219 2019 GMT)") ));               
$response_time = strtotime($matches[1]);

But the result is null , Any ideas please ? Thank You !

Upvotes: 2

Views: 47

Answers (1)

Nick
Nick

Reputation: 147166

You have a couple of issues, you shouldn't be attempting to make a DateTime object directly from the string (that's what \DateTime::createFromFormat is for) and you are missing the required e format character to match the GMT part of the string. Try this:

$date = \DateTime::createFromFormat('M d H:i:s.u Y e', "Apr 11 16:21:29.219 2019 GMT");
echo $date->format('Y-m-d H:i:s.u');

Output:

2019-04-11 16:21:29.219000

Demo on 3v4l.org

Upvotes: 5

Related Questions