2013-02-14 41 views
0

我試圖解析Google日曆以在我們的電視上使用來顯示'今日事件'。單個日期標題下的「今日事件」組?

雖然我很感謝朋友的幫助,但我想看看有沒有人能幫助我。

下面的代碼會生成包含所有信息的日曆,但是對於每個條目它都會顯示日期。因爲他們都是同一天,所以在看這個時候會感到沮喪和困惑。我遠不及程序員,但我可以理解一些事情。

如何將所有今天的活動分組在一個日期標題下?

在此先感謝。

<?php 
    $confirmed = 'http://schemas.google.com/g/2005#event.confirmed'; 
    $three_months_in_seconds = 60 * 60 * 24 * 28 * 3; 
    $three_months_ago = date("Y-m-d\Th:i:s", time() - $three_months_in_seconds); 
    $three_months_from_today = date("Y-m-d\Th:i:s", time() + $three_months_in_seconds); 
    $params = "?orderby=starttime&start-min=" . $three_months_ago . "&start-max=" . $three_months_from_today; 
//$params = "?orderby=starttime&start-min=2012-12-01T05:48:47&start-max=2013-05-07T05:48:47&sortorder=a&singleevents=true&futureevents=true"; 
$params = "?orderby=starttime&sortorder=a&singleevents=true&futureevents=true"; 
    $feed = "https://www.google.com/calendar/feeds/REDACTED%40gmail.com/private-REDACTED/full".$params; 
    $doc = new DOMDocument(); 
    if (!$doc->load($feed)) echo 'failed to load'; 
    $entries = $doc->getElementsByTagName("entry"); 
    foreach ($entries as $entry) { 

     $status = $entry->getElementsByTagName("eventStatus"); 
     $eventStatus = $status->item(0)->getAttributeNode("value")->value; 

     if ($eventStatus == $confirmed) { 
      $titles = $entry->getElementsByTagName("title"); 
      $title = $titles->item(0)->nodeValue; 

      $times = $entry->getElementsByTagName("when"); 
      $startTime = $times->item(0)->getAttributeNode("startTime")->value; 
      $when = date("D M j, Y", strtotime($startTime)); 
      $time = date("g:i A",strtotime($startTime)); 


      $places = $entry->getElementsByTagName("where"); 
      $where = $places->item(0)->getAttributeNode("valueString")->value; 

      print "<div class='row when'>$when</div>"; 
      echo "<div class='row event'><span class='time'>$time</span><span class='title'>$title</span><span class='where'>$where</span></div>"; 
//   print $where . "\n"; 
      print "\n"; 
     } 
    } 
?> 

回答

1

有一個答案:

只是改變這一點:

print "<div class='row when'>$when</div>"; 

這樣:

if ($old_when!=$when) print "<div class='row when'>$when</div>"; $old_when=$when; 

,並添加

$old_when = null; 

之前的foreach