We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

change date format to a datefield?

In my html code is being generated date in the following format "yyyy-mm-dd", I need to save it in the format 'DD / MM / YYYY'. This is because my database is Oracle. How could I change it?

Add in beforeSave:

// if it's already DateTime object:
$this->date = $this->date->format('d/m/Y');
//if not create one:
$this->date = (new DateTime($this->date))->format('d/m/Y');


81.2k

How do I set my function for variables? $CONT_FECNACI; $CONT_FECINSC

this is my model <?php

class SpmContacto extends \Phalcon\Mvc\Model
{

public $CONT_CODIGO;
public $CONT_CEDULA;
public $CONT_RUCIDE;
public $CONT_NOMBRE;
public $CON_ESTADO;
public $CONT_TELEFO;
public $CONT_DIRECC;
public $CONT_AREA;
public $CONT_CARGO;
public $CONT_TIPOXX;
public $CONT_EMAIL;
public $CONT_USUARIO;
public $CONT_CLAVE;
public $CONT_CLAVEE;
public $CONT_FECNACI;
public $CONT_FECINSC;
public $CONT_TIPOCODIGO;

/**
 * Initialize method for model.
 */
public function initialize()
{
    $this->setSchema("SPOLS");
    $this->hasMany('CONT_CODIGO', 'SPMREFERENCIA', 'CONT_CODIGO', array('alias' => 'SPMREFERENCIA'));
    $this->hasMany('CONT_CODIGO', 'SPTDETALLE', 'CONT_CODIGO', array('alias' => 'SPTDETALLE'));
    $this->hasMany('CONT_CODIGO', 'SPTENCABEZADO', 'CONT_CODIGO', array('alias' => 'SPTENCABEZADO'));
}
public function getSource()
{
    return 'SPM_CONTACTO';
}
public static function find($parameters = null)
{
    return parent::find($parameters);
}
public static function findFirst($parameters = null)
{
    return parent::findFirst($parameters);
}

}

edited Apr '16

Just add:

public function beforeSave()
{
    // do something with $this->CONT_FECNACI
    // do something with $this->CONT_FECINSC
}

to your class SpmContacto



81.2k

I add the following

 public function beforeSave()
{
    // if it's already DateTime object:
    $this->$CONT_FECNACI = $CONT_FECNACI->date->format('dd/mm/YYYY');
    $this->$CONT_FECINSC = $CONT_FECINSC->date->format('dd/mm/YYYY');

}

but the result was:

500 Internal Server Error

edited Apr '16

So just check the log. You just added hell a wrong code. First learn php before using any framework beacause it looks like you don't even know plain php and OOP etc.



81.2k

No, I do not know all PHP commands, so I'm learning. It is the first really to work with a framework. But not knowing is no reason not to learn

edited Apr '16

Yes it's reason good enough to not learn FRAMEWORK. For first learn PLAIN PHP and object and OOP. For isntance:

$this->$CONT_FECNACI // there is no $CONT_FECNACI variable in beforeSave() function
$this->$CONT_FECINSC // there is no $CONT_FECINSC variable in beforeSave() function
$CONT_FECNACI->date->format('dd/mm/YYYY'); // there is no $CONT_FECINSC variable in beforeSave() function, also are you sure that $CONT_FECNACI have property with name date ?
$CONT_FECINSC->date->format('dd/mm/YYYY'); // there is no $CONT_FECINSC variable in beforeSave() function, also are you sure that $CONT_FECNACI have property with name date ?

Those problems are not framework related. You just wrote wrong code and you could wrote it in any framework. First learn plain php, objects, classes etc - then start with framework.



81.2k

Thank you very much for your help and have a different result continuare consulting

public function beforeSave()
    {
    $CONT_FECNACI = (new DateTime($CONT_FECNACI))->format('d/m/Y');
    $CONT_FECINSC = (new DateTime($CONT_FECNACI))->format('d/m/Y');
    }

DateTime::__construct(): Failed to parse time string (25/04/2016) at position 0 (2): Unexpected character

You already have 25/04/2016 so it's d/m/Y ?