I'm trying to convert the time that cakephp send but I can't do it. When I try to convert the output gives me the date of 01-01-1970.
I wrote some echos in different styles to se the output. Print screenshot
The 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>
Controller:
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);
}
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>';
}
?>
Have you looked in to the class Cake\\I18n\\Time. This comes very handy when dealing with Date/Time, You will still have the same format in the database like "2017-02-28 18:23:02" but if you wan to render "Feb 28, 2017, 6:23 PM" then you can use the I18 "nice()" function in your .ctp file.
Take a look at the documentation on the link bellow...
https://book.cakephp.org/3.0/en/core-libraries/time.html#Cake \\I18n\\Time::nice
Hope this helps
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.