จัดรูปแบบวันที่บน cakephp 3.0

ฉันกำลังพยายามแปลงเวลาที่ cakephp ส่งแต่ทำไม่ได้ เมื่อฉันพยายามแปลงผลลัพธ์จะให้วันที่ 01-01-1970.

ฉันเขียนเสียงสะท้อนในสไตล์ที่แตกต่างกันเพื่อดูผลลัพธ์ พิมพ์ภาพหน้าจอ

ป้อนคำอธิบายรูปภาพที่นี่

เพจ:

<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);

}

person Denis Ciprian Otoroschi    schedule 19.06.2017    source แหล่งที่มา


คำตอบ (2)


ฉันคาดหวังว่าสถานที่ของคุณทำให้วัตถุ date จัดรูปแบบเอาต์พุตในลักษณะที่ strtotime ไม่ชอบเป็นอินพุต มีเหตุผลใดที่คุณใช้ strtotime ทุกที่แทนที่จะปล่อยให้วัตถุข้อมูลทำสิ่งนั้น?

<?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>';
}
?>
person Greg Schmidt    schedule 19.06.2017
comment
รูปแบบฐานข้อมูลจะเป็น YYYY-mm-dd และถ้าฉันทำเสียงสะท้อน รูปแบบที่เค้กจะพิมพ์จะเป็น dd/mm/YY ดังที่คุณเห็นบนหน้าจอการพิมพ์ที่ฉันสร้าง เสียงสะท้อนแรกคือวันนี้ ที่สองคือวันที่ที่จัดรูปแบบจากฐานข้อมูล ที่สามคือเสียงสะท้อนของวันที่จากฐานข้อมูล ฉันใช้ strtotime เพื่อลองจัดรูปแบบข้อมูล - person Denis Ciprian Otoroschi; 20.06.2017
comment
อาจเป็นเพราะวิธีที่ฉันส่งข้อมูลจากคอนโทรลเลอร์ไปยังมุมมองหรือไม่ - person Denis Ciprian Otoroschi; 20.06.2017
comment
เมื่ออ่านวันที่จากฐานข้อมูล Cake ควรแปลงเป็นวัตถุ ซึ่งสามารถจัดรูปแบบ การเปรียบเทียบ ฯลฯ โดยไม่จำเป็นต้องใช้ strtotime หากคุณ pr($ementas['data_ementa']); คุณควรเห็นว่ามันเป็นวัตถุมากกว่าสตริง และโค้ดของฉันด้านบนควรแสดงว่าคุณสามารถใช้มันเพื่อวัตถุประสงค์ของคุณได้อย่างไร หาก pr แสดงเป็นสตริง แสดงว่าปัญหาอยู่ที่อื่น อาจอยู่ในการตั้งค่าฐานข้อมูลหรือสคีมาของคุณ (เช่น หากคุณจัดเก็บไว้ในฟิลด์สตริงแทนที่จะเป็นฟิลด์วันที่) - person Greg Schmidt; 20.06.2017
comment
ใช่ มันพิมพ์เป็นวัตถุ วัตถุเค้ก\I18n\FrozenDate ( [เวลา] =› 2017-06-29T00:00:00+00:00 [เขตเวลา] =› UTC [fixedNowTime] =› ) - person Denis Ciprian Otoroschi; 20.06.2017
comment
และคุณได้ลองใช้การเปลี่ยนแปลงโค้ดของฉันด้านบนเพื่อดูว่าจะช่วยแก้ปัญหาของคุณหรือไม่? - person Greg Schmidt; 20.06.2017
comment
ใช่ มันทำให้การเรียกฟังก์ชันรูปแบบสมาชิก () มีข้อผิดพลาดสตริง - person Denis Ciprian Otoroschi; 21.06.2017
comment
แก้ไขปัญหาแล้ว ฉันใช้ $data = date('d-m-Y'); ดังนั้นฉันจึงเปลี่ยนเป็น $data = new DateTime(); ขอบคุณสำหรับเวลาของคุณ ชื่นชมมาก :) - person Denis Ciprian Otoroschi; 21.06.2017

คุณได้ดูคลาส Cake\I18n\Time แล้วหรือยัง สิ่งนี้มีประโยชน์มากเมื่อต้องจัดการกับวันที่/เวลา คุณจะยังคงมีรูปแบบเดียวกันในฐานข้อมูลเช่น "2017-02-28 18:23:02" แต่ถ้าคุณต้องการแสดงผล "28 ก.พ. 2560, 18:23 น. " จากนั้น คุณสามารถใช้ฟังก์ชัน I18 "nice()" ในไฟล์ .ctp ของคุณได้

ดูเอกสารตามลิงค์ด้านล่างครับ...

https://book.cakephp.org/3.0/en/core-libraries/time.html#Cake\I18n\Time::nice

หวังว่านี่จะช่วยได้

person Rod    schedule 20.06.2017