当datetime格式为“Y-m-d H:i:s”时,FullCalendar日历不显示事件。

时间:2020-12-21 21:27:33

I am trying to make a FullCalendar calendar (that I have integrated in my application using Symfony2) display the loaded events from the database. In fact, when I have succeeded to do that using the code below which is the code of the functions which displays the events into the calendar:

我正在尝试做一个完整的日历(使用Symfony2集成在我的应用程序中)显示来自数据库的加载事件。实际上,当我成功地使用下面的代码时,它是将事件显示到日历中的函数代码:

public function LoadeventsAction() {
    $eventg = new eventsgroupe();
    $securityContext = $this->get('security.context');
    $token = $securityContext->getToken();
    $user = $token->getUser();
    $id = $user->getId();
    $em = $this->getDoctrine()->getManager();


    $groupe=$this->getRequest('groupe'); 

    $idg = intval($groupe->attributes->get('id'));

    $qb = $em->createQueryBuilder();

    $qb->select('e')
            ->from('IkprojGroupeBundle:eventsgroupe', 'e')
            ->where(' e.idEventGroupe = :ig');
    //$qb->setParameter("i", $id);
    $qb->setParameter("ig", $idg);
           $query = $qb->getQuery();
    $event = $query->getResult();



    $rows = array();
    foreach ($event as $obj) {
        $rows[] = array(
            'id' => $obj->getId(),
            'title' => "'" . $obj->getTitle() . "'",
            'start' => '"' . $obj->getStart()->format('Y-m-d') . '"',

            'end' => '"' . $obj->getEnd()->format('Y-m-d') . '"',

            'location' => '"' . $obj->getLocation(). '"',
            'description' => '"' . $obj->getDescription(). '"',
            //ajoute dees informations concernant levenement

        );
    }
    $response = new Response(json_encode($rows));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

This is the screenshot of the calendar that I have: 当datetime格式为“Y-m-d H:i:s”时,FullCalendar日历不显示事件。 As you can notice above, the calendar is displayed in month view. Please focus on the events on the date: September 3rd 2014. Actually, when I click on the button "day" I obtain this view: 当datetime格式为“Y-m-d H:i:s”时,FullCalendar日历不显示事件。 As you can see at the screenshot above, all the events are "All day" events (which means events which last the whole day) whereas each one of them starts at a specific time and ends at a specific time as well. For example this is the record of the event "learning css" in the database (the last one): 当datetime格式为“Y-m-d H:i:s”时,FullCalendar日历不显示事件。 As you see at the screenshot above, the datetime fields have this format: 'Y-m-d H:i:s'. As I need time to display the events correctly in the day view (instead of being displayed as "all day" events), I changed the code of the function (I mean the first one I put in this post). Actually, I put 'start' => '"' . $obj->getStart()->format('Y-m-d H:i:s') . '"', instead of 'start' => '"' . $obj->getStart()->format('Y-m-d') . '"', and I put 'end' => '"' . $obj->getEnd()->format('Y-m-d H:i:s') . '"', instead of 'end' => '"' . $obj->getEnd()->format('Y-m-d') . '"', . So my new code is the following one:

这是我所拥有的日历的截图:正如您可以在上面看到的,日历在月视图中显示。请关注日期:2014年9月3日实际上,当我点击按钮“天”我获得这一观点:在上面的截图中,你可以看到,所有的活动都是在“整天”事件(这意味着事件的最后一天),而每个人都是从一个特定的时间和结束在一个特定时间。例如,这是数据库中事件“学习css”的记录(最后一个):正如您在上面的屏幕截图中看到的,datetime字段有这样的格式:“Y-m-d H:i:s”。由于我需要时间在day视图中正确地显示事件(而不是显示为“all day”事件),所以我更改了函数的代码(我是指我在本文中放入的第一个函数)。实际上,我输入'start' => ' '。$ obj - > getStart()- >格式(Y-m-d H:我:年代”)。而不是'start' => ' '。$ obj - > getStart()- >格式(“Y-m-d”)。' ' ',然后我把'end' => ' ' ' '。$ obj - > getEnd()- >格式(Y-m-d H:我:年代”)。而不是'end' => ' '。$ obj - > getEnd()- >格式(“Y-m-d”)。”,。我的新代码如下:

class  EventgroupeController extends Controller

{

{

 public function LoadeventsAction() {
    $eventg = new eventsgroupe();
    $securityContext = $this->get('security.context');
    $token = $securityContext->getToken();
    $user = $token->getUser();
    $id = $user->getId();
    $em = $this->getDoctrine()->getManager();


    $groupe=$this->getRequest('groupe'); 

    $idg = intval($groupe->attributes->get('id'));

    $qb = $em->createQueryBuilder();

    $qb->select('e')
            ->from('IkprojGroupeBundle:eventsgroupe', 'e')
            ->where(' e.idEventGroupe = :ig');
    //$qb->setParameter("i", $id);
    $qb->setParameter("ig", $idg);
           $query = $qb->getQuery();
    $event = $query->getResult();



    $rows = array();
    foreach ($event as $obj) {
        $rows[] = array(
            'id' => $obj->getId(),
            'title' => "'" . $obj->getTitle() . "'",

            'start' => '"' . $obj->getStart()->format('Y-m-d H:i:s') . '"',

            'end' => '"' . $obj->getEnd()->format('Y-m-d H:i:s') . '"',
            'location' => '"' . $obj->getLocation(). '"',
            'description' => '"' . $obj->getDescription(). '"',
            //ajoute dees informations concernant levenement

        );
    }
    $response = new Response(json_encode($rows));
    $response->headers->set('Content-Type', 'application/json');
    return $response;
}

Please focus on this code part:

请关注以下代码部分:

foreach ($event as $obj) {
        $rows[] = array(
            'id' => $obj->getId(),
            'title' => "'" . $obj->getTitle() . "'",

            'start' => '"' . $obj->getStart()->format('Y-m-d H:i:s') . '"',

            'end' => '"' . $obj->getEnd()->`format('Y-m-d H:i:s') . '"',`
            'location' => '"' . $obj->getLocation(). '"',
            'description' => '"' . $obj->getDescription(). '"',
            //ajoute dees informations concernant levenement

        );
    }

The problem is that since I have changed the code like that, the calendar doesn't display any events in any view. It is obvious that the issue comes from this code part: format('Y-m-d H:i:s') . '"',. So my question is: what is the correct code? and how to deal with events that have datetime with this format 'Y-m-d H:i:s'?

问题是,因为我已经更改了这样的代码,所以日历不会在任何视图中显示任何事件。很明显,这个问题来自这个代码部分:格式('Y-m-d H:i:s')。”,。所以我的问题是:什么是正确的代码?以及如何处理具有这种格式的datetime的事件:i:s ?

1 个解决方案

#1


1  

I'm assuming you are using Carbon for date format. If so, your issue is not with the dates, but with your object values. You don't need to add "'" at the beginning and end of each field.

我假设你用的是碳的日期格式。如果是,您的问题不是与日期有关,而是与您的对象值有关。您不需要在每个字段的开头和结尾添加“'”。

Change your foreach to this:

改变你的每一个:

foreach($event as $obj) {
    $rows[] = array(
        'id'          => $obj->getId(),
        'title'       => $obj->getTitle(),
        'start'       => $obj->getStart()->format('Y-m-d H:i:s'),
        'end'         => $obj->getEnd()->format('Y-m-d H:i:s'),
        'location'    => $obj->getLocation(),
        'description' => $obj->getDescription(),
        //ajoute dees informations concernant levenement
    );
}

If you append the '. your JSON object will have this format:

如果你附加了。JSON对象的格式如下:

[{"id":"387","title":"'Learning PHP'","start":"\"2014-09-03 13:00:00\"","end":"\"2014-09-03 15:00:00\"","location":"\"at home\"","description":"\"learning classes lesson\""}]

And if you remove that character, it will have the following format (which works correctly):

如果您删除了该字符,它将具有以下格式(正确地工作):

[{"id":"387","title":"Learning PHP","start":"2014-09-03 13:00:00","end":"2014-09-03 15:00:00","location":"at home","description":"learning classes lesson"}]

#1


1  

I'm assuming you are using Carbon for date format. If so, your issue is not with the dates, but with your object values. You don't need to add "'" at the beginning and end of each field.

我假设你用的是碳的日期格式。如果是,您的问题不是与日期有关,而是与您的对象值有关。您不需要在每个字段的开头和结尾添加“'”。

Change your foreach to this:

改变你的每一个:

foreach($event as $obj) {
    $rows[] = array(
        'id'          => $obj->getId(),
        'title'       => $obj->getTitle(),
        'start'       => $obj->getStart()->format('Y-m-d H:i:s'),
        'end'         => $obj->getEnd()->format('Y-m-d H:i:s'),
        'location'    => $obj->getLocation(),
        'description' => $obj->getDescription(),
        //ajoute dees informations concernant levenement
    );
}

If you append the '. your JSON object will have this format:

如果你附加了。JSON对象的格式如下:

[{"id":"387","title":"'Learning PHP'","start":"\"2014-09-03 13:00:00\"","end":"\"2014-09-03 15:00:00\"","location":"\"at home\"","description":"\"learning classes lesson\""}]

And if you remove that character, it will have the following format (which works correctly):

如果您删除了该字符,它将具有以下格式(正确地工作):

[{"id":"387","title":"Learning PHP","start":"2014-09-03 13:00:00","end":"2014-09-03 15:00:00","location":"at home","description":"learning classes lesson"}]