How to parse a date string in PHP?

2019-01-18 11:05发布

With a date string of Apr 30, 2010, how can I parse the string into 2010-04-30 using PHP?

标签: php date parsing
2条回答
女痞
2楼-- · 2019-01-18 11:23

Try http://php.net/manual/en/function.strtotime.php to convert to a timestamp and then http://www.php.net/manual/en/function.date.php to get it in your own format.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-18 11:41

Either with the DateTime API (requires PHP 5.3+):

$dateTime = DateTime::createFromFormat('F d, Y', 'Apr 30, 2010');
echo $dateTime->format('Y-m-d');

or the same in procedural style (requires PHP 5.3+):

$dateTime = date_create_from_format('F d, Y', 'Apr 30, 2010');
echo date_format($dateTime, 'Y-m-d');

or classic (requires PHP4+):

$dateTime = strtotime('Apr 30, 2010');
echo date('Y-m-d', $dateTime);
查看更多
登录 后发表回答