1

I want to explode a date but want to rename default index 0,1,2 to year, month, day respectively, I tried but I am not able to figure it out. Here's what am doing right now.

$explode_date = explode("-", "2012-09-28");
echo $explode_date[0]; //Output is 2012
echo $explode_date[1]; //Output is 09
echo $explode_date[2]; //Output is 28

what I want

echo $explode_date['year']; //Output is 2012
echo $explode_date['month']; //Output is 09
echo $explode_date['day']; //Output is 28

Thanks..

4

6 に答える 6

6
list($date['year'], $date['month'], $date['day']) = explode('-', '2012-09-28');

http://php.net/list

于 2012-07-13T09:14:34.433 に答える
6

array_combineを使用します。

$keys = array('year', 'month', 'day');
$values = explode("-", "2012-09-28");
$dates = array_combine($keys, $values);
于 2012-07-13T09:15:29.453 に答える
1
list($year, $month, $day)  = explode("-", "2012-09-28");
$x = compact('year', 'month', 'day');


var_dump($x);
array
  'year' => string '2012' (length=4)
  'month' => string '09' (length=2)
  'day' => string '28' (length=2)
于 2012-07-13T09:14:37.197 に答える
0
$explode_date = array();
list($explode_date['year'],$explode_date['month'],$explode_date['day']) = explode("-", "2012-09-28");

var_dump($explode_date);
于 2012-07-13T09:16:13.180 に答える
0
$explode_date = array (
    'year' => $explode_date [0],
    'month' => $explode_date [1],
    'day' => $explode_date [2]
);
于 2012-07-13T09:14:57.367 に答える
0

関連付けをマップする必要があります。

$explode_date = explode("-", "2012-09-28");
$new_array['year'] = $explode_date[0];
$new_array['month'] = $explode_date[1];
$new_array['day'] = $explode_date[2];

または、PHP の組み込みの DateTime クラスを使用することもできます (やりたいことは既に行われているので、おそらくより良いでしょう):

http://www.php.net/manual/en/book.datetime.php

$date = new DateTime('2012-09-28');
echo $date->format('Y');
echo $date->format('m');
echo $date->format('d');
于 2012-07-13T09:18:15.697 に答える