ZF2, confirming the PT_BR date and time format always. Input does not appear to be a valid date

Project using ZF2 + Doctrine 2.

I have tried many formats. I am working with a form without validation.

My last try:

    $traindate = new Element\DateTime('trainDate');
    $traindate->setAttributes(array(
        'name' => 'trainDate',
        'id' => 'trainDate',
        'size' => '30',
        'class' => 'datepicker',
        'options' => array(
            'label' => '',
            'format' => 'd-m-Y H:i'
        ),
    ));

      

I need to use input to set the date and time of an event. In Brazil, the basic format: 14-05-2014 14:20 15-05-2015 15:00

With the help of days "Months", "Years", "Minutes", as I say in "Options → Format".

This way whenever I try to insert, I get the following message: Input does not appear to be a valid date

By changing the format, I can only pass $ form-> isValid ($ data) to Ymd (American Format), but by the way, I am also unable to pass the time to date, which is causing me big problems.

I need to set PT_BR date to Form / Input, Pass by validation, Convert Data do Mysql Format (YYYY-mm-dd HH: ii: ss). And then extract from db and convert back to pt_br format.

But even I cant pass time with date to zf2 form, ever this error message.

I am removing all filters from this form trying to get it working but not working.

Where is the main problem?

0


source to share


2 answers


After drawing my attention to this problem for a long time, I found a correct and quick solution. After 6 months, I learned:


Everything is correct:

    $traindate = new Element\DateTime('trainDate');
    $traindate->setAttributes(array(
        'name' => 'trainDate',
        'id' => 'trainDate',
        'size' => '30',
        'class' => 'datepicker',
    ));
    $traindate->setFormat('d/m/Y'); //ONLY WORKS ON THIS FORMAT.

      



Documents and people over the Internet do not provide any clarity, but only this form works for setting the format.

And to capture this in Entity, you need to write your own Hydrator that extends DoctrineHydrator:

namespace Application\Hydrator;
use DoctrineModule\Stdlib\Hydrator\DoctrineObject;

class MyCustomHydrator extends DoctrineObject {
    protected function handleTypeConversions($value, $typeOfField) 
    {

        if($typeOfField == 'datetime'){
            return \DateTime::createFromFormat('d/m/Y', $value);
        }

        return parent::handleTypeConversions($value, $typeOfField);
    }
}

      

This makes it easy to work with any date format. You can continue to further execute Locale statements on this Custom Hydrator as you wish.

+1


source


I recommend that you perform a date to train conversion operation . The trainData property strong> getter and setter can be:

//THE PROPERTY IS IN MYSQL FORMAT, BUT THE GETTER WILL RETURN IT ALWAYS IN THE BRAZILIAN ONE
public function getTrainDateTime() {
    return $this->trainDateTime->format( 'd/m/Y H:i' );
}

//IT WILL ALWAYS RECIEVE A BRAZILIAN DATETIME, CONVERT IT TO MYSQL FORMAT
public function setTrainDateTime( $trainDateTime ) {
    $time = \DateTime::createFromFormat( 'd/m/Y H:i', $trainDateTime )->getTimestamp();

    $this->trainDateTime = new \DateTime( date( 'Y-m-d', $time ) );

    return $this;
}

      



If you do it this way, you can always work freely with Brazilian dates without worrying about formats. The dirty work will be done by the entity class.

0


source







All Articles