简体   繁体   English

Symfony2 日期转换 (dd.mm.yyyy) 到 (yyyy-mm-dd)

[英]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.我是Symfony2PHP 的新手,我有一个关于格式化日期的问题。

I have a form where the user can enter a date in this format: dd.mm.yyyy .我有一个表单,用户可以在其中输入以下格式的日期: dd.mm.yyyy The SQL Database uses this format: yyyy-mm-dd . SQL 数据库使用此格式: yyyy-mm-dd Now when make an sql query i get a mismatch of course.现在,当进行 sql 查询时,我当然会遇到不匹配的情况。

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 ?将输入dd.mm.yyyy日期转换为所需格式yyyy-mm-dd的最简单方法是什么? Does there exist some global solution if I'd need this transformation more than 1 time?如果我需要这种转换超过 1 次,是否存在一些全局解决方案? I prefer a non-JavaScript solution.我更喜欢非 JavaScript 解决方案。

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:尝试指明参数类型并确保$birthdate变量是一个\\DateTime对象:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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