How to convert this string into array?

Hi guys,

I have this very basic string.


2000-12-20

The first 4 digits are year
The 2nd 2 digits are month
The last 2 digits are day

I know this is very basic.
Can you help me convert this into array,

array(‘year’ => 2000,
‘month’ => 12,
‘day’ => 20);

Thank you very much in advanced.

$date = '2000-12-20';
$array = explode('-', $date);

:slight_smile:

thanks dude.

you asked for an associative array. If you really need it, use:

$date = '2000-12-20';
$temp = explode('-', $date);

$array = array('year' => $temp[0], 
               'month' => $temp[1],
               'day' => $temp[2]);

:slight_smile:

sscanf() to the rescue!

sscanf('2000-12-20', '%d-%d-%d', $array['year'], $array['month'], $array['day']);

If you don’t mind having a bunch of other keys/values in the array, then another option is:

$array = date_parse('2000-12-20');

Thanks guys very nice answers.