rich_traff Posted June 14, 2011 Share Posted June 14, 2011 I have sets of date ranges stored as yyyy-mm-dd example; start date: 2011-05-26 end date: 2011-05-28 i have these stored in an array of arrays, print_r gives this; Array ( [0] => Array ( [0] => 2011-05-26 [1] => 2011-05-28 ) [1] => Array ( [0] => 2011-05-28 [1] => 2011-05-30 ) [2] => Array ( [0] => 2011-06-13 [1] => 2011-06-24 ) ) I need to rearrange the date format to mm/dd/yyyy whilst keeping the order of the array intact. Note changing the '-' to an '/' is important. I then need to be able to echo the date ranges separated by a comma, so the above array becomes 05/26/2011-05/28/2011, 05/28/2011-05/30/2011, 06/13/2011-06/24/2011 can anyone help with this? Quote Link to comment https://forums.phpfreaks.com/topic/239318-change-yyyy-mm-dd-date-ranges-to-mmddyyyy-from-array/ Share on other sites More sharing options...
Fadion Posted June 14, 2011 Share Posted June 14, 2011 Just a quick code that gets the job done. It's tested, but you can take a look for any possible optimisation. <?php /* ** Date Conversion */ $dates = array(array('2011-05-26', '2011-05-28'), array('2011-05-28', '2011-05-30')); function convertDate (&$array, $key) { foreach ($array as &$v) { list($year, $month, $day) = explode('-', $v); $v = $month . '/' . $day . '/' . $year; } } array_walk($dates, 'convertDate'); //print_r($dates); /* ** Dates Printing */ $dates_print = ''; foreach ($dates as $array) { $values = array_values($array); $dates_print .= implode('-', $values) . ', '; } $dates_print = trim($dates_print, ', '); //echo $dates_print; ?> Quote Link to comment https://forums.phpfreaks.com/topic/239318-change-yyyy-mm-dd-date-ranges-to-mmddyyyy-from-array/#findComment-1229456 Share on other sites More sharing options...
rich_traff Posted June 14, 2011 Author Share Posted June 14, 2011 thanks GuiltyGear, you've saved me a lot of searching much appreciated Quote Link to comment https://forums.phpfreaks.com/topic/239318-change-yyyy-mm-dd-date-ranges-to-mmddyyyy-from-array/#findComment-1229484 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.