簡體   English   中英

用PHP組織日期

[英]Organising dates in PHP

我想以以下格式組織一系列日期。 作為一點背景,日期是我的用戶可以添加的即將發生的事件的日期。 格式:

2011年1月

日期-事件

日期-事件

日期-事件

2011年2月

日期-事件

日期-事件

日期-事件

以及給定月份中的所有事件,並按發生日期排列。 我所有的活動日期都以unix時間戳記存儲在數據庫中。 問題是,我想知道id從哪里開始開發可以用這種方式對所有事件進行排序的函數。 誰能幫我嗎?

干杯

我喜歡先使用數組索引對項目進行分組。

控制者

$months = array();
foreach ($rows as $row) {
    $month = date('F Y', strtotime($row['date']);

    if (!isset($months[$month]) {
        $months[$month] = array();
    }

    $months[$month][] = $row;
}

視圖

foreach ($months as $month => $events) {
    echo '<h2>' . $month . '</h2>';
    foreach ($events as $event) {
        // ...
    }
}

重復事件; 跟蹤最近一次看到的MONTH。 如果當前月份=上個月,則顯示月份分隔符。

$lastDate = '';

foreach ($events as $e)
{   
    $currDate = date('F Y', $e->date);
    if ($currDate != $lastDate )
    {
        echo "<h1>$currDate</h1>";
    }
    // might want to format the date to be human readable here...
    echo "<li>$e->date, $e->event"; 
    $lastDate = $currDate;

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM