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";
        }
    }
?>
4

1 回答 1

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 之前

于 2013-02-14T21:44:36.417 回答