Array string in php

Does anyone know how to group a group of arrays into one variable? I'm trying to hide the date format in a different format, but I need to bind to 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 the end results to be:

"2008-5-11"

      

But I dont know how to group the variables together, can anyone help?

0


source to share


4 answers


For this problem, try using the PHP default date function.

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

      

Or a long short answer,



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

      

Or as another guy said,

$new_date = implode('-', $date_parts1);

      

+6


source


Reverse 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];

      

+4


source


Like this?


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

      

+1


source


Here's the function:

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

}

      

After that, just call the function with:

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

      

Note: the input date separator is "/" here, but you can change it anywhere.

-1


source







All Articles