php - Omitting redundant months and years in date range list -
i have got strange issue dates of events , have tried hard fixed unable it.
i attaching screenshot of how want display dates on page :
in picture first event deine energie in aktion! combination of 5 events each event having start date , end date.
the first part of event 1 day event starts on 4th april , ends on 4th april. second part on 7th april, 3rd part on 9th april , 4th part on 20th april
the last part starts on 5th may , ends on 10th may.
the dates stored in database in format : showing dates last part of event. event start date : 2013-05-05 00:00:00 event end date : 2013-05-10 00:00:00
so want display dates in format shown in picture.
there multiple cases: first if dates coming within single month display month name @ end once. second if months changed month name shown after date when month changed.
i getting events dates in while loop, how compare current event date coming event date in loop.
this code have used far dates database..
$nid = $row->nid; $get_product_id = "select product_id {uc_product_kits} 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 {event} 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 {event} nid=%d group ",$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; } }
easiest first collect data query e.g. array.
only iterate on array. having data allow compare 2 consecutive date ranges decide level of details need print each.
commented example:
// collect data sql query structure 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"), ); // actual code range list generation: ($i = 0; $i < count($events); $i++) { // parse start , end of range $this_event = $events[$i]; $this_start_date = strtotime($this_event["event_start"]); $this_end_date = strtotime($this_event["event_end"]); // extract months , 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 , end of next range, if 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 different starting , ending months go // on 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 if differs 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 { // range starting , ending in same month echo date("j", $this_start_date); // different starting , ending day if ($this_start_date != $this_end_date) { echo "-".date("j", $this_end_date); } $newline = false; // print month last range; // , range starts(=ends) in different month // next range ends if ($last || ($this_start_month != $next_end_month)) { echo " ".date("m", $this_start_date); $newline = true; } // print year last range; // , range starts(=ends) in different year // 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) printed future range // on same line echo ", "; } } }
this outputs:
4, 7, 9, 20 apr <br/> 5-10 may 2013 <br/> 1-2 jan 2014 <br/>
Comments
Post a Comment