How to compare dates from the database with the dates in the calendar twig?

时间:2015-06-15 14:34:18

标签: php symfony date twig

I would like compare the dates in the database with the dates on the calendar.

In my controller I get all dates:

$employments = $this->getDoctrine()->getRepository('Bundle:Employment')
            ->findActualEmployments();

$freedays = $this->getDoctrine()->getRepository('Bundle:FreeDays')
            ->findBy(array(), array('date'=>'asc' )  );
foreach($freedays as $free){
     $list_freedays[] = date('Y-m-d',strtotime($free->getDate()->format('Y-m-d')));
        }
$days = array();
    foreach($freedays as $free){
    for($i=1;$i<=$maxDay;$i++)
    {
        $days[$i] = array(
          "date" => date('Y-m-d',strtotime($year.'-'.$month.'-'.$i)), 
          "name_en" => date('D',strtotime($year.'-'.$month.'-'.$i)),
          "name_trans" => 'main.'.strtolower(date('D',strtotime($year.'-'.$month.'-'.$i))),
          "free_day" => $list_freedays
        );}}
return $this->render('Bundle:Holiday:index.html.twig',array(
        'employments'=>$employments,
        'days' => $days,
        'list_freedays' => $list_freedays,
        'form' => $form->createView()
         ));

In My Twig I try to compare dates like below:

<tbody>
            <tr>
                <th id="col2" align="center">
                    {% set time = "now"|date("U","Europe/Warsaw") %}

                    {{['main',date('2015-' ~ j ~ '-01') |date('F')|lower]|join('.')|trans({},'Bundle')}}
                </th>

        {% for number,day in days %}

                        {#mark weekend#}
                        {% if  day.name_trans   == 'SAT'  %}    

                            <th align="center" id="col4" > 

                        {%elseif day.name_trans  == 'SUN'%}
                            <th align="center" id="col4" >

                        {%elseif (day.date) == (day.free_day[3])%}
                            {{day.free_day[3]}}  
                            <th align="center" id="col4" >
                        {%else%} 
                            <th align="center" id="col3" >                                            
                        {% endif  %}      
                                 {# day content here #} 
                      {% if numday in range(1,daysInMonth) %}   
                              {{ numday }}
                              {%set numday = numday + 1 %}  

                              {{ day.name_trans }}</th>   

                       {%endif%}    
                      {%endfor%}

            </tr>
        </tbody>

Everything works well if in this line "{%elseif (day.date) == (day.free_day[3])%}" I put an numbers from 0 to 3 because I have four dates in array. How compare to all of the dates of the array, not just one. When I change this line to "{%elseif (day.date) == (day.free_day)%}" then I got error "Notice: Array to string conversion in..." Please help.

2 个答案:

答案 0 :(得分:2)

您可以尝试in_array method

//True if day.date is in day.free_day
{%elseif (in_array (day.date , day.free_day))%}

如果您愿意,还可以设置一个可选的布尔参数strict。这样也可以检查数组匹配类型中的项目。

答案 1 :(得分:1)

我找到了一个非常简单的解决方案。我改变了这一行:

{%elseif (day.date) == (day.free_day[3])%}

到此:

{%elseif (day.date) in (day.free_day)%}

感谢您的帮助