简体   繁体   中英

Symfony2 Date transforming (dd.mm.yyyy) to (yyyy-mm-dd)

I'm new with Symfony2 and PHP and I've a question pertaining to formatting dates.

I have a form where the user can enter a date in this format: dd.mm.yyyy . The SQL Database uses this format: yyyy-mm-dd . Now when make an sql query i get a mismatch of course.

public function getFindingPatientQuery($birthdate){
    $em = $this->getEntityManager();
    $query = $em->createQuery(
            'SELECT p
            FROM Data\LiveBundle\Entity\DataAPatient p
            WHERE p.pDateOfBirth = :birthdate'
        )->setParameter('birthdate', $birthdate| ('Y-m-d'));

    return $query;
}

What's the easiest way to transform the input dd.mm.yyyy date to the demanded format yyyy-mm-dd ? Does there exist some global solution if I'd need this transformation more than 1 time? I prefer a non-JavaScript solution.

I found some answer here in the forum but nothing worked.

Thanks

May be your birthdate isn't the type date ?

$date = DateTime::createFromFormat('j-M-Y', $birthdate);
echo $date->format('Y-m-d');

Try to indicate parameter type and make sure the $birthdate variable is a \\DateTime object:

use Doctrine\DBAL\Types\Type;
...
->setParameter('birthdate', $birthdate, Type::DATE);

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM