2013-04-29 138 views
0

我從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); 
          } 
         } 
        } 
       } 
      } 
+0

您不能使用'timeMax'和'timeMin'參數來事件列表方法來選擇特定日期的事件嗎? – Barmar 2013-04-29 16:54:30

+0

每個事件應該有一個start_date和end_date(可能在1天內是一樣的),然後用'WHERE mydate BETWEEN start_date AND end_date'進行搜索。 Simples – Waygood 2013-04-29 16:56:00

+0

也可以使用'$ twodates = explode('to',$ date_string);'將日期分割。然後'$ datestart = strtotime($ twodates [0]);'創建'date(「Y-m-d」,$ datestart)'可以使用的時間戳。 – Waygood 2013-04-29 17:03:51

回答

0

這將循環遍歷事件和echo匹配事件(您需要創建數組)。

function groupEvents($mydate='2013-03-05') 
{ 
    foreach($this->events as $event) 
    { 
     // Date is in the format of "Mon Apr 29, 2013 to Wed May 1, 2013". 
     $event_date=$event->getDate(); 
     // check if TO exists in the event date 
     if(stristr($event_date, ' to ')) 
     { 
      $twodates=explode(" to ", $event_date); 
      $start_date=strtotime($twodates[0]); 
      $end_date=strtotime($twodates[1]); 

      if((date("Y-m-d", $start_date)<=$mydate) && ($mydate<=date("Y-m-d", $end_date))) 
      { 
       // assuming $event->getName() will get the name of the event 
       echo 'Event '.$event->getName().' '.date("m/d/Y", $start_date).' to '.date("m/d/Y", $start_date); 
      } 
     } 
     else 
     { 
      $start_date=strtotime($event_date); 

      if(date("Y-m-d", $start_date)==$mydate) 
      { 
       // assuming $event->getName() will get the name of the event 
       echo 'Event '.$event->getName().' '.date("m/d/Y", $start_date); 
      } 
     } 
    } 
} 
0

我想接近它的方式是首先解析日曆文本到一個數組。爲了使事情更容易,我會申報Event課程來組織每個活動並存儲相關信息。我會在Event類中聲明一個類似is_active的方法,該方法將DateTime參數作爲其唯一參數,並根據日期是否落在事件的開始/結束日期內返回truefalse。如果你需要的話,從那裏很容易過濾你現有的所有事件數組成特定日期的子集。

類似下面應該讓你開始:

class Event { 
    // instance variables. 
    private $_name; 
    private $_begin; 
    private $_end; 

    // retrieve the name of the current Event instance. 
    public function name() { return $this->_name; } 

    // build a new immutable Event instance 
    public function __construct($name, $begin, $end) { 
     $this->_name = $name; 
     $this->_begin = $begin; 
     $this->_end = $end; 
    } 

    // check whether the $date argument falls between this Event's start/end dates. 
    // $date is assumed to be a valid DateTime object 
    public function is_active($date) { 
     return $date >= $this->_begin && $date <= $this->_end; 
    } 
} 

現在,繼續以更OOP設計,我們還需要創建一個類來管理一組事件中,我將使用Calendar這個案例。我已經添加了一個靜態方法來解析文件中的日曆。

class Calendar { 
    // Regular Expression for parsing the date range of an event. Update this if the format changes. 
    // currently it assumes strings like: "Date: mm/dd/YYYY - mm/dd/YYYY" 
    const PREG_DATE = '~^Date:\\s*(\\d{1,2}/\\d{1,2}/\\d{2,4})\\s*-\\s*(\\d{1,2}/\\d{1,2}/\\d{4})~i'; 
    // for date_create_from_format, matches the above Regular Expressions assumptions about date ordering 
    const DATE_FORMAT = 'm/d/Y'; 

    // hold onto all the events 
    private $_events; 

    public function __construct($evts) { 
     $this->_events = $evts; 
    } 

    // Retrieve all events active on a certain date. 
    // $date is assumed to be a valid DateTime object 
    public function get_events($date) { 
     // If using PHP less than 5.3, you'll need to rewrite this without using closures. 
     // basically just filters the array according to the results of calling "is_active" on each 
     // of the event objects. 
     return array_filter($this->_events, function($obj) use($date) { 
      return $obj->is_active($date); 
     }); 
    } 

    // Very simple parsing function to read a Calendar from file 
    // fairly rigid formatting expected. 
    // 
    // Event Name 
    // Date: formatted date <-- must be on the very next line 
    // <any number of blank lines can follow the above name/dates pair> 
    // ... 
    public static function from_file($filepath) { 
     $events = null; 
     $fs = fopen($filepath, 'r'); 

     if ($fs !== false) { 
      $events = array(); 
      while (($buffer = fgets($fs)) !== false) { 
       $buffer = trim($buffer); 
       if ($buffer !== "") { // skip empty lines 
        if (($dateString = fgets($fs)) === false || preg_match(self::PREG_DATE, $dateString, $matches) !== 1) { 
         break; // Invalid format reached (either premature EOF or unrecognized date format 
        } 

        // Create a new Event object and add it to the array with the parsed values. 
        $events[] = new Event(
         $buffer, 
         date_create_from_format(self::DATE_FORMAT, $matches[1]), 
         date_create_from_format(self::DATE_FORMAT, $matches[2]) 
        ); 
       } 
      } 
      fclose($fs); 
     } 

     return $events !== null ? new Calendar($events) : null; 
    } 
} 

那樣容易。日曆控制着一組Event對象。通過調用get_events,您可以檢索在特定日期活動的所有事件的數組。例如:

$calendar = Calendar::from_file('foo.txt'); 
var_dump($calendar->get_events(date_create_from_format('m/d/Y', '5/03/2013'))); 
相關問題