在cakephp 3.0上格式化日期

时间:2017-06-19 09:11:08

标签: date cakephp-3.0

我正在尝试转换cakephp发送的时间,但我不能这样做。 当我尝试转换输出时,我会得到01-01-1970.

的日期

我写了一些不同风格的回声来输出。 打印屏幕截图

enter image description here

Page:

<tfoot>
        <tr>
        <?php 
        $data = date('d-m-Y');
        //$data2 = new DateTime($ementas['data_ementa']);
        ?>
            <td class="marcacaobutton button-<?=$ementas['id']?>">
                <?php 
                echo date('d-m-Y',strtotime($data));
                echo "<br>";
                echo date('d-m-Y',strtotime($ementas['data_ementa']));
                echo "<br>";
                echo $ementas['data_ementa'];
                echo "<br>";
                    if(date('d-m-Y',strtotime($data)) > date('d-m-Y',strtotime($ementas['data_ementa'])) && $query['status']=='desmarcada')
                    {
                        echo '1';
                        //echo "<td>".$this->Html->link(__('Bloquear'),'/admin/users/bloquear/'.$user['id'],['class' => 'btn btn-danger btn-sm'])."</td>";
                    }
                    else if(date('d-m-Y',strtotime($data)) > date('d-m-Y',strtotime($ementas['data_ementa'])) && $query['status']=='marcada')
                    {
                        echo '2';
                    }
                    else if(date('d-m-Y',strtotime($data)) < date('d-m-Y',strtotime($ementas['data_ementa'])) && $query['status']=='desmarcada')
                    {
                        echo '3<button type="button" class="marcar" id="marcar-'.$ementas['id'].'" >Marcar</button>';
                        //echo "<td>".$this->Html->link(__('Bloquear'),'/admin/users/bloquear/'.$user['id'],['class' => 'btn btn-danger btn-sm'])."</td>";
                    }
                    else if(date('d-m-Y',strtotime($data)) < date('d-m-Y',strtotime($ementas['data_ementa'])) && $query['status']=='marcada')
                    {
                        echo '4<button type="button" class="desmarcar" id="desmarcar-'.$ementas['id'].'">Desmarcar</button>';
                        //echo "<td>".$this->Html->link(__('Ativar'),'/admin/users/ativar/'.$user['id'],['class' => 'btn btn-success btn-sm'])."</td>";
                    }
                ?>
            </td>
        </tr>
    </tfoot>

控制器:

public function marcar($id)
{
    $this->loadModel('Refeicoes');
    $id_user = $this->request->session()->read('Auth.User.id');
    $ementa = $this->Ementas->get($id);
    //die(debug($ementa));
    $id_ementa = $ementa->id;
    //  die(debug($id_ementa));

    $query = $this->Refeicoes->find('all', [
        'conditions' => ['Refeicoes.user_id LIKE' => $id_user, 'Refeicoes.ementa_id LIKE' => $id_ementa]
    ]);
    $number = $query->count();
    //die(debug($number));
    $date =  new Time();
    if ($number == 0) {

        $marc_refeicao = $this->Refeicoes->newEntity();
        $marc_refeicao = $this->Refeicoes->patchEntity($marc_refeicao, array('user_id' => $id_user, 'data' => $date, 'status' => 'desmarcada'));
        $marc_refeicao->ementa_id = $id_ementa;
        //die(debug($marc_refeicao));
        $this->Refeicoes->save($marc_refeicao);
    }

    $ementas = $ementa; 
    $query1 = $this->Refeicoes->get($id);
    //die(debug($ementas));   
    $this->set('ementas',$ementas); 
    $this->set('query',$query1);

}

2 个答案:

答案 0 :(得分:0)

I expect that your locale is causing the date object to format output in a way that strtotime doesn't like as input. Any particular reason you're using strtotime everywhere instead of just letting the data objects do their thing?

<?php
echo $data->format('d-m-Y');
echo "<br>";
echo $ementas['data_ementa']->format('d-m-Y');
echo "<br>";
echo $ementas['data_ementa'];
echo "<br>";
if($data > $ementas['data_ementa'] && $query['status']=='desmarcada')
{
    echo '1';
}
else if($data > $ementas['data_ementa'] && $query['status']=='marcada')
{
    echo '2';
}
else if($data < $ementas['data_ementa'] && $query['status']=='desmarcada')
{
    echo '3<button type="button" class="marcar" id="marcar-'.$ementas['id'].'" >Marcar</button>';
}
else if($data < $ementas['data_ementa'] && $query['status']=='marcada')
{
    echo '4<button type="button" class="desmarcar" id="desmarcar-'.$ementas['id'].'">Desmarcar</button>';
}
?>

答案 1 :(得分:0)

你有没有看过Cake \ I18n \ Time课程。这在处理日期/时间时非常方便,您仍然可以在数据库中使用相同的格式,如“2017-02-28 18:23:02”,但如果您要渲染“2017年2月28日,下午6:23” “那么你可以在你的.ctp文件中使用I18”nice()“函数。

请查看以下链接上的文档......

https://book.cakephp.org/3.0/en/core-libraries/time.html#Cake \的I18n \时间::好的

希望这有帮助