2013-04-18 65 views
6

我遇到了有關事件日期的奇怪問題,並且我盡力使其得到解決,但無法完成。省略日期範圍列表中的冗餘月份和年份

我附上的我多麼想在頁面上顯示的日期的截圖: enter image description here

在圖片中AKTION第一事件Deine科特布斯!是5個事件的組合,每個事件都有其開始日期和結束日期。

活動的第一部分是1日活動,活動從4月4日開始,到4月4日結束。第二部分同樣在4月7日,4月9日第3部分和4月20日第4部分

最後一部分從5月5日開始到5月10日結束。

日期以這種格式存儲在數據庫中: 我正在顯示事件的最後部分的日期。 活動開始日期:2013年5月5日00:00:00 活動結束日期:2013年5月10日00:00:00

所以我想顯示在圖片中顯示的日期格式。

有多種情況: 首先,如果所有日期都在一個月內發生,那麼我們只在最後一次顯示月份名稱。 二是如果月份發生變化,月份名稱將在月份更改日期之後顯示。

我在while循環中獲取事件日期,所以如何將當前事件日期與循環中即將到來的事件日期進行比較。

這是迄今爲止我已經習慣了從數據庫中獲取日期的代碼..

$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; 
    } 
} 

回答

4

最簡單的做法是首先將查詢中的所有數據收集到陣列。

只有然後遍歷數組。將所有數據放在一起將允許您比較兩個連續日期範圍,以決定每個日期需要打印的詳細程度。

評論例如:

// 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 ", "; 
     } 
    } 
} 

此輸出:

4, 7, 9, 20 Apr <br/> 
5-10 May 2013 <br/> 
1-2 Jan 2014 <br/> 
+0

我已經更新了我的一些示例代碼的答案。 – 2013-04-18 09:44:33

+1

謝謝馬丁。這對我有效。 – 2013-04-18 12:41:47

+0

不客氣。快樂,它幫助你。 – 2013-04-18 12:46:52

0

好吧,既然你需要從一個循環到另一個比較值,你就不能直接使用echo

您需要使用臨時變量。因此,在開始日期的第一個循環中,您存儲了$tmp_day_1$tmp_month_1,然後使用結束日期循環,您可以比較兩個月並檢查它們是否有差異。然後你可以使用echo。我希望我的觀點:)

1

,以檢查一種可能性,如果你需要打印一個月的當前日期的項目實際上是在下一個要檢查的項目。讓我試着用僞代碼解釋:

<?php 

    $month = 0; // Initialize $month variable to unset 

    // Loop over all your events 
    foreach($dates as $date) { 

     // Convert $date to a timestamp 

     // If the 'month' of the current $timestamp is unequal to $month 
     // it means we switch months and we have to print the $month first 
     if(date('m', $timestamp) != $month) { 

      echo $month; // Of course format how you want it to be displayed 

      // Set $month to the new month 
      $month = date('m', $timestamp); 
     } 

     // Print the rest of the event, like day numbers here 

    } 
?> 
相關問題