Php Symfony2和1969/04/27日期

Php Symfony2和1969/04/27日期,php,symfony,symfony-forms,Php,Symfony,Symfony Forms,我有一张表格: // AppBundle\Form\MyFormType.php //... ->add('startDate', 'date', array( 'widget'=>'single_text' )) //.... 除了日期为1969/04/27之外,一切正常 我在Symfony2的2.6和2.7版本上进行了测试。这两个问题是一样的。我还在不同的应用程序中进行了测试,问题也很相似。这是因为您使用的是php In

我有一张表格:

// AppBundle\Form\MyFormType.php
//...
->add('startDate', 'date', array(               
    'widget'=>'single_text'        
))
//....
除了日期为1969/04/27之外,一切正常




我在Symfony2的2.6和2.7版本上进行了测试。这两个问题是一样的。我还在不同的应用程序中进行了测试,问题也很相似。

这是因为您使用的是php Intl。我使用symfony Intl解决了这个问题。 这是我使用的代码。 在类DateTimeToLocalizedStringTransformer.php中

use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;



class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
{
     ....
     ....
     ....

     /**
     * Returns a preconfigured IntlDateFormatter instance.
     *
     * @return IntlDateFormatter
     *
     * @throws TransformationFailedException in case the date formatter can not be constructed.
     */
    protected function getIntlDateFormatter()
    {
        $dateFormat = $this->dateFormat;
        $timeFormat = $this->timeFormat;
        $timezone = $this->outputTimezone;
        $calendar = $this->calendar;
        $pattern = $this->pattern;
        //remove
        //$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
        //add
        $intlDateFormatter = new IntlDateFormatter('en', $dateFormat, $timeFormat, $timezone, $calendar, $pattern);

        // new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
        if (!$intlDateFormatter) {
            throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
        }

        $intlDateFormatter->setLenient(false);

        return $intlDateFormatter;
    }
}

但这只适用于语言环境“en”,直到它在计算机科学中的其他语言环境中实现为止。1970年以前的日期通常是古怪的。1970年之前的其他日期有效吗?是的,除1969/04/27之外的所有日期如果给定值(此处
startDate
)不是字符串、无法解析日期或不支持输入时区,则会发生此错误。(如=>
reverseTransform
中所述)。
use Symfony\Component\Intl\DateFormatter\IntlDateFormatter;



class DateTimeToLocalizedStringTransformer extends BaseDateTimeTransformer
{
     ....
     ....
     ....

     /**
     * Returns a preconfigured IntlDateFormatter instance.
     *
     * @return IntlDateFormatter
     *
     * @throws TransformationFailedException in case the date formatter can not be constructed.
     */
    protected function getIntlDateFormatter()
    {
        $dateFormat = $this->dateFormat;
        $timeFormat = $this->timeFormat;
        $timezone = $this->outputTimezone;
        $calendar = $this->calendar;
        $pattern = $this->pattern;
        //remove
        //$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
        //add
        $intlDateFormatter = new IntlDateFormatter('en', $dateFormat, $timeFormat, $timezone, $calendar, $pattern);

        // new \intlDateFormatter may return null instead of false in case of failure, see https://bugs.php.net/bug.php?id=66323
        if (!$intlDateFormatter) {
            throw new TransformationFailedException(intl_get_error_message(), intl_get_error_code());
        }

        $intlDateFormatter->setLenient(false);

        return $intlDateFormatter;
    }
}