我從Google日曆導入事件並解析它們以在其他地方顯示。我遇到了跨越一系列日期的事件。例如:根據PHP中的日期對事件數組進行排序
事件1
日期:2013年4月29日 - 2013年5月3日事件2
日期:2013年5月1日 - 2013年5月3日事件3
日期:2013年5月3日 - 2013年5月6日
當我顯示2013年5月3日,我需要有事件1,2和3顯示了事件。
到目前爲止,我的計劃是生成一個DatePeriod,其中包含事件開始和結束日期之間的所有日期,然後遍歷它,在日期的關鍵字下添加事件信息。換句話說,我會有一個包含事件數組的日期數組。
這是我到目前爲止。我認爲我走在正確的道路上,但任何幫助將不勝感激。謝謝。
public function groupEvents()
{
foreach($this->events as $event)
{
// Date is in the format of "Mon Apr 29, 2013 to Wed May 1, 2013".
if(preg_match_all("/\w{3} \w{3} \d{0,2}. \d{4}/", $event->getDate(), $matches))
{
// If there is more than one match then the event spans multiple days.
if(count($matches[0] > 1))
{
// Following line grabs the first date in the format of "Apr 29 2013"
$bDate = substr($matches[0][0],4,6) . substr($matches[0][0],11);
// Following line grabs the last date in the format of "May 1,2013"
$eDate = substr($matches[0][1],4,6) . substr($matches[0][1],11);
// Create a new DateTime based on the beginning date
$begin = new DateTime($bDate);
// Create a new DateTime based on the ending date
$end = new DateTime($eDate);
// Interval of 1 day.
$interval = new DateInterval('P1D');
// Create a DatePeriod of all dates between the beginning and end dates
$period = new DatePeriod($begin,$interval,$end);
foreach($period as $d)
{
// Problems start...
$this->newList[$d] = array($d => $newEvent);
}
}
}
}
}
您不能使用'timeMax'和'timeMin'參數來事件列表方法來選擇特定日期的事件嗎? – Barmar 2013-04-29 16:54:30
每個事件應該有一個start_date和end_date(可能在1天內是一樣的),然後用'WHERE mydate BETWEEN start_date AND end_date'進行搜索。 Simples – Waygood 2013-04-29 16:56:00
也可以使用'$ twodates = explode('to',$ date_string);'將日期分割。然後'$ datestart = strtotime($ twodates [0]);'創建'date(「Y-m-d」,$ datestart)'可以使用的時間戳。 – Waygood 2013-04-29 17:03:51