Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Omitting redundant months and years in date range list

I have got strange issue with dates of events and I have tried hard to get it fixed but unable to do it.

I am attaching a screenshot of how I want to display the dates on the page : enter image description here

In the picture the first event Deine Energie in Aktion! is a combination of 5 events with each event having its start date and end date.

The first part of the event is 1 day event which starts on 4th April and ends on 4th April. Similarly the second part is on 7th April, 3rd part on 9th April and 4th part on 20th April

The last part starts on 5th May and ends on 10th May.

The dates are stored in database in this format : I am showing the dates for last part of event. Event Start Date : 2013-05-05 00:00:00 Event End Date : 2013-05-10 00:00:00

So I want to display dates in the format shown in the picture.

There are multiple cases: First is if all the dates are coming within a single month then we display the month name at the end only once. Second is if months are changed then the month name will be shown after the date when the month is changed.

I am getting events dates in a while loop, so how do I compare the current event date with the coming event date in a loop.

This is the code I have used so far to get the dates from the database..

    $nid = $row->nid;  
$get_product_id = "SELECT product_id from {uc_product_kits} where nid='$nid'";
$res = db_query($get_product_id);
while ($get_product_id_array_value = db_fetch_array($res)) {
    $prductid = $get_product_id_array_value['product_id'];
    $start_date = db_query("select event_start,event_end from {event} where nid=%d",$prductid);
    $start_date_value = db_fetch_object($start_date);
    $end_value = $start_date_value->event_start;

    $event_end_date = $start_date_value->event_end;
    $TotalStart =  date("d M Y", strtotime($end_value));
    $TotalEnd = date("d M Y", strtotime($event_end_date));
    $onlyMonthStart =  date("M", strtotime($end_value));
    $onlyMonthEnd = date("M", strtotime($event_end_date));
    //$groupMonth = db_query("select event_start,event_end, month  from {event} where nid=%d group by ",$prductid);
    if($TotalStart == $TotalEnd ){
        $startDay = date("d", strtotime($end_value));
        $startMonth = date("M", strtotime($end_value));
        if(in_array($startMonth,$newMonth)) {
            echo $onlstartdate;
        } 
        else {
              $onlstartdate =  date("d", strtotime($end_value));
             echo $onlstartdate;
             $tempStorage[] = $startMonth
        }
        //$newMonth[] = $startMonth;
    }
}
like image 741
samir chauhan Avatar asked Apr 18 '13 06:04

samir chauhan


1 Answers

Easiest would be to first collect all data from your query into e.g. array.

Only then iterate over the array. Having all data together will allow you to compare two consecutive date ranges to decide level of details you need to print for each.

Commented example:

// collect data from SQL query into structure like this:

$events = array(
    array("event_start" => "2013-4-4", "event_end" => "2013-4-4"),
    array("event_start" => "2013-4-7", "event_end" => "2013-4-7"),
    array("event_start" => "2013-4-9", "event_end" => "2013-4-9"),
    array("event_start" => "2013-4-20", "event_end" => "2013-4-20"),
    array("event_start" => "2013-5-5", "event_end" => "2013-5-10"),
    array("event_start" => "2014-1-1", "event_end" => "2014-1-2"),
);

// the actual code for range list generation:

for ($i = 0; $i < count($events); $i++)
{
    // parse start and end of this range
    $this_event = $events[$i];
    $this_start_date = strtotime($this_event["event_start"]);
    $this_end_date = strtotime($this_event["event_end"]);

    // extract months and years
    $this_start_month = date("M", $this_start_date);
    $this_end_month = date("M", $this_end_date);
    $this_start_year = date("Y", $this_start_date);
    $this_end_year = date("Y", $this_end_date);

    $last = ($i == count($events) - 1);
    // parse start and end of next range, if any
    if (!$last)
    {
        $next_event = $events[$i + 1];
        $next_start_date = strtotime($next_event["event_start"]);
        $next_end_date = strtotime($next_event["event_end"]);

        $next_start_month = date("M", $next_start_date);
        $next_end_month = date("M", $next_end_date);
        $next_start_year = date("Y", $next_start_date);
        $next_end_year = date("Y", $next_end_date);
    }

    // ranges with different starting and ending months always go
    // on their own line
    if (($this_start_month != $this_end_month) ||
        ($this_start_year != $this_end_year))
    {
        echo date("j M", $this_start_date);
        // print starting year only if it differs from ending year
        if ($this_start_year != $this_end_year)
        {
            echo " ".date("Y", $this_start_date);
        }
        echo "-".date("j M Y", $this_end_year)." <br/>\n";
    }
    else
    {
        // this is range starting and ending in the same month

        echo date("j", $this_start_date);
        // different starting and ending day
        if ($this_start_date != $this_end_date)
        {
            echo "-".date("j", $this_end_date);
        }

        $newline = false;
        // print month for the last range;
        // and for any range that starts(=ends) in different month
        // than the next range ends
        if ($last ||
            ($this_start_month != $next_end_month))
        {
            echo " ".date("M", $this_start_date);
            $newline = true;
        }

        // print year for the last range;
        // and for any range that starts(=ends) in different year
        // than next range ends
        if ($last ||
            ($this_start_year != $next_end_year) ||
            ($next_start_month != $next_end_month))
        {
            echo " ".date("Y", $this_start_date);
            $newline = true;
        }

        if ($newline)
        {
            echo " <br/>\n";
        }
        else
        {
            // month (and year) will be printed for some future range
            // on the same line
            echo ", ";
        }
    }
}

This outputs:

4, 7, 9, 20 Apr <br/>
5-10 May 2013 <br/>
1-2 Jan 2014 <br/>
like image 81
Martin Prikryl Avatar answered Oct 12 '22 23:10

Martin Prikryl