With a date string of Apr 30, 2010
, how can I parse the string into 2010-04-30
using PHP?
Asked
Active
Viewed 5.1k times
22

Danny Beckett
- 20,529
- 24
- 107
- 134

user295515
- 877
- 3
- 8
- 12
2 Answers
65
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);

Gordon
- 312,688
- 75
- 539
- 559
13
Try strtotime() to convert to a timestamp and then date() to get it in your own format.

mickmackusa
- 43,625
- 12
- 83
- 136

zaf
- 22,776
- 12
- 65
- 95
-
1There's also a `date_parse` function that converts a string into an array with keys `year`, `month`, `day`, etc. – wecsam Mar 27 '17 at 17:21