Dear Friends,

I have a string with structure

$selectedTime = "2017-02-10 07:00,2017-02-10 08:00,2017-02-11 09:00";

I need to convert it as


any idea ? please advise asap


Dear Friends,

With the Help of my Friend Vishnu A.R I solved the issue

my required Code is

$selectedTime = "2017-02-10 08:00,2017-02-10 07:00,2017-02-11 09:00";

$arr1 = explode(",", $selectedTime);

$arr2 = array();

foreach($arr1 as $time){
  $strtime = strtotime($time);
  $arr2[$strtime] = $time;
ksort($arr2); // sorting array in ascending order of key value
$arr3 = array();
foreach($arr2 as $key => $time){
  $timesplit = explode(" ", $time);

  $arr3[$timesplit[0]][] = array('start' => $timesplit[1]);
$output = json_encode($arr3);



commented: Here I thought a regular expression may do. This works too. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.