SilverStripe函数中变量生成的格式化日期

时间:2015-07-15 17:39:37

标签: php date silverstripe

我有一个函数用于从SilverStripe管理员获取一些事件数据,其中一个变量用于保存日期。我想格式化日期,但是我现在如何设置,我看到每个事件连续两次输出日期:

public function getCalendarEvents(){
    if(Director::is_ajax()){
        $allEvents = array();

        foreach(CalendarEvents::get() as $Event){
            $date = $Event->EventDate.date('m-d-Y');//this is the variable I want to format
            $title = $Event->EventTitle;
            $link = $Event->EventLink;
            $time = $Event->EventTime;

            if(!array_key_exists($date, $allEvents)){
            $allEvents[$date] = array(
                array($time, $title)
              );
            }
            else{
                $existingEvents = $allEvents[$date];
                array_push($existingEvents, array(
                    array($time, $title)
                ));
                $allEvents[$date] = $existingEvents;
            }
        }
        return json_encode($allEvents);
    }
    else{
      return CalendarEvents::get();
    }
}

这个函数产生的例子是:

  

{&#34; 2015-07-0207-15-2015&#34;:[[&#34; 18:00:00&#34;,&#34;知识就是力量&#34;]],< / p>

如何编辑代码以防止日期显示两次?

1 个答案:

答案 0 :(得分:4)

$Event->EventDate.date('m-d-Y')

是两个字符串的串联吗?

$ Event-&gt; EventDate(SS日期)与日期连接(&#39; m-d-Y&#39;)(此php日期)

也许你应该......

$date = date('m-d-Y',strtotime($Event->EventDate))