Date formatting is changing its parameters

Asked

Viewed 63 times

1

I’m using callback beforeSave to format the date of d/m/Y for Y-m-d. The date is being saved as Y-d-m instead of Y-m-d. Syntactically the code is right.

The date is in the table as date.

function beforeSaveData

Watch me thrash $data before formatting and after formatting. Before formatting, the date is correct, after formatting, the month takes the value of the day and the value of the day takes the month. Ex: 10/01/2016, it looks like: 2016-10-01, that is, it is the first of October.

Appmodel:

public function beforeSaveData($data) {
    debug($data);
    $data = date('Y-m-d', strtotime($data));
    debug($data); die();
    return $data;$data;
}

Model Despesa:

public function beforeSave($options=>array()) {
    $this->data['Despesa']['valor_despesa'] = $this->beforeSaveValor($this->data['Despesa']['valor_despesa']);

    $this->data['Despesa']['data_despesa'] = $this->beforeSaveData($this->data['Despesa']['data_despesa']);
    return true;
}

On the controller:

public function inserirDespesa() {
    if ($this->request->isPost()) {

        if ($this->Despesa->save($this->request->data)) {

            $this->Session->setFlash("Despesa inserida");
        }

        else {
            $this->Session->setFlash("Despesa não inserida");
        }
    }
}

And also follow the view. The date in the view is like type=text because I have regular duties in javascript for date and monetary value.

<html>
<body>
<?php
echo $this->element('menuDespesas');

//echo $this->Html->script('inserir_despesa');
?>

<script>

function mascara(o, f) {
obj=o;
fun=f;
setTimeout("execMascara()", 1);
}

function execMascara() {
obj.value=fun(obj.value);
}

function data(data) {
data=data.replace(/\D/g,""); // Remove tudo o que não é dígito//
data=data.replace(/^(\d{2})(\d)/,"$1/$2"); // Coloca barra entre o 2° e o 3°
                                            // digito//
data=data.replace(/(\d{2})(\d)/,"$1/$2"); // Coloca barra entre o 4° e o 5°
                                            // dígito//
return data;
}

function valor(valor){
valor=valor.replace(/\D/g,"");
valor=valor.replace(/(\d)(\d{2})$/,"$1,$2");
return valor;
}

</script>

<h2>INSERIR DESPESA</h2><p>

<?php
echo $this->Session->flash();

echo $this->Form->create('Despesa');

echo $this->Form->input('despesa', array('label'=>'Despesa:', 'maxlength'=>'30'));

//campo valor_despesa do tipo texto para aceitar a "," (vírgula).

echo $this->Form->input('valor_despesa', array('label'=>'Valor (apenas números, incluindo os centavos. Coloque 00 quando não houver centavos):', 'maxlength'=>'10', 'type'=>'text', 'onkeypress'=>"mascara(this, valor)"));

//data_despesa com type=text para possibilitar a digitação e o símbolo de "/".
echo $this->Form->input('data_despesa', array('label'=>'Data:', 'type'=>'text', 'maxlength'=>'10', 'onkeypress'=>"mascara(this, data)" ));

echo $this->Form->input('local_despesa', array('label'=>'Local da despesa', 'maxlength'=>'15'));

echo "<p>";
echo $this->Form->submit('Inserir despesa');
echo $this->Form->button('Limpar', array('type'=>'reset'));
echo $this->Form->end();

?>
</body>
</html>
  • Have you tried beforeSaveData manual with explodes?

  • 1

    Make your code available directly on the question, not in an external service. If it goes off the air, we have no way to view it.

  • @Guilherme Lopes I didn’t think about it! I’ll do it here.

  • !Celsomtrindade prefer to put no hastebin or Pastebin that avoids the editing problems here in the stack and it is easier to put the code, besides the question does not get extensive.

  • To not get so extensive the question, put only the important parts of the doubt, often many companies block external sites ai ends up complicating for those who want to help because the site may be blocked or off the air as @Celsom said

2 answers

3

Here is an implementation for date that will possibly solve your problem:

public function beforeSaveData($data)
{
   $data = explode("/", $data);
   $novaData = $data[2] . "-" . $data[1] . "-" . $data[0];
   return $novaData;    
}

follows example of the code: https://ideone.com/P4NwPB

  • 1

    !André Nascimento ok, it worked! I hadn’t thought about the explosion!

2


Use the class DateTime to do the formatting:

$data = DateTime::createFromFormat('d/m/Y', $data);
return $data->format('Y-m-d')

Browser other questions tagged

You are not signed in. Login or sign up in order to post.