tags:

views:

165

answers:

4

Does anyone know how to string a group of arrays into a single variable? I am trying to covert the a date format into another format, but i need to string to the arrays.

$datetochange="2008-11-5"; $date_parts=explode("-", $datetochange);

//Displays 2008 print $date_parts[0];

//Displays 11 print $date_parts[1];

//Displays 5 print $date_parts[2];

//Stringing the Arrays together $dateData = $date_parts1[0] + $date_parts1[2] +$date_parts1[1];

I want end results to be: "2008-5-11" But I do not know how to string the variables together, can anyone help?

+4  A: 

The reverse of explode(), implode():

$temp = $date_parts[2];
$date_parts[2] = $date_parts[1];
$date_parts[1] = $temp;
implode('-',$date_parts);

Or concatenation:

$dateString = $date_parts[0] . '-' . $date_parts[2] . '-' . $date_parts[1];
Eran Galperin
+5  A: 

For that problem, try using PHP's default date() function.

$new_date = date("Y-d-m", strtotime($datetochange));

Or long answer short,

$date_parts1[0] . '-' . $date_parts1[2] . '-' . $date_parts1[1];

Or like the other guy said,

$new_date = implode('-', $date_parts1);
Matt
+1  A: 

Like this?


   $date = "2008-11-05";
   $new_date = date("Y-d-m", strtotime($date));
Andy
A: 

Here's a function:

function date_convert($date){
 $data=explode("/",$date);
 $date_1=array($data['2'],$data['1'],$data['0']);
 return $date_unix=implode("-",$date_1);

}

After this just call the function with:

$date=date_convert(22/1/2008);

Note: the input date delimiter here is "/", but you can change it in whatever you want.

dede