我从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