首页 > 解决方案 > How to parse date string to dateTime in php?

问题描述

I have a date string in this format "2014-12-01 16:00:02,153". How do I parse this to a datetime format for mysql?

I am using laravel and Carbon. My code is Carbon::parse("2014-12-01 16:00:02,153");

but get an error that reads DateTime::__construct(): Failed to parse time string (2014-12-01 16:00:02,153) at position 20 (1): Unexpected character

标签: phpdatetimephp-carbon

解决方案


You could use the createFromFormat method from the DateTime object :

$dateTime = DateTime::createFromFormat('Y-m-d H:i:s,u', "2014-12-01 16:00:02,153");
echo $dateTime->format('Y-m-d H:i:s');

推荐阅读