asifa
asifa

Reputation: 771

displaying the dates in a particular format php

I have a date array sorted in asc order. I want to display the date as

Oct 10,12,24 2012
Dec 12,20,24 2012
Jan 02,10,25 2013

I have got the dates as the month ie.Oct,Dec,Jan.. and the dates and year, but i want it to be displayed in the above mentioned format. I have tried the below code, but it is not giving the desired result. Can someone pls help me with this? $CruiseDetailsSailing is the array containing the date in an ascending order.

  if (count($CruiseDetailsSailing) > 0) {
                            $date = array();
                            $month = array();
                            $Year = array();
                            for ($n = 0; $n < count($CruiseDetailsSailing); $n++) {
                                if ($CruiseDetailsSailing[$n] != "") {
                                    $date[] = date('Y-m-d', strtotime($CruiseDetailsSailing[$n]));
                                }
                            }
                        }
                        sort($date);
                        if (count($date) > 0) {
                            $temp = "";
                            $yeartemp = "";
                            for ($p = 0; $p < count($date); $p++) {
                                $month = date("M", strtotime($date[$p]));
                                $day = date("d", strtotime($date[$p]));
                                $year = date("Y", strtotime($date[$p]));
                                if ($month != $temp) {
                                    $temp = $month;
                                    ?>
                                    <li> <?php
                        echo $temp . " " . $day . ", ";
                    } else {
                        echo $day . ", ";
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) == (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) == (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                    if (($p != 0) && ((date("M", strtotime($date[$p]))) != (date("M", strtotime($date[$p - 1])))) && ((date("Y", strtotime($date[$p]))) != (date("Y", strtotime($date[$p - 1]))))) {
                        echo $year;
                    }
                }

Oct is the month and 10,12,24 are the months and 2012 is the year. I got the the dates as Oct 10 2012,Oct 12 2012,Oct 24 2012. I just want to display the results as Oct only once and then the dates and the year just once. Thanks,

Upvotes: 1

Views: 961

Answers (2)

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You can do it simply like this

/* Array to pass the function   
$date_array =   array(
                '2012-10-10',
                '2012-12-10',
                '2012-24-10'
                );
*/

function getMyDateFormat($date_array){

    $days   =   array();
    $year   =   '';
    $month  =   '';

    sort($date_array);  

    if(count($date_array)>0){


        foreach($date_array as $row)
        {
            $days[] =   date('d',strtotime($row));
            $year   =   date('Y',strtotime($row));
            $month  =   date('M',strtotime($row));
        }

        $new_date   =   $month  .' '.   implode(',',$days)  .' '.   $year;
        return  $new_date;
    }
}   

Upvotes: 0

MarcDefiant
MarcDefiant

Reputation: 6889

this should do:

sort($date); // sorted input array containing dates

$result_dates = Array();

// bring dates in correct format
foreach($date as $current_date) {
  $month = date("M", strtotime($current_date));
  $day = date("d", strtotime($current_date));
  $year = date("Y", strtotime($current_date));
  $result_dates[$year][$month][] = $day;
}

// output dates
foreach($result_dates as $year => $months) {
  foreach($months as $month => $days) {
    echo $month . " " . implode(",", $days) . " " . $year . "\n";
  }
}

the following input:

$date = Array('10/03/2012', '10/10/2012', '10/17/2012', '11/04/2012', '11/05/2012', '11/23/2012');

results in:

Oct 03,10,17 2012
Nov 04,05,23 2012

Upvotes: 1

Related Questions