请阅读我的第三次更新,因为现在的问题是处理Symfony中的DST
我的MedicalLeaveType
中有以下表格:
$builder->add('recDate', 'date', array(
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'input' => 'datetime',
'label' => "Leave's Reception Date"
))->add('startDate', 'date', array(
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'input' => 'datetime',
'label' => "Leave's Start Date"
))->add('endDate', 'date', array(
'widget' => 'single_text',
'format' => 'dd/MM/yyyy',
'input' => 'datetime',
'label' => "Leave's End Date"
))->add('totDays', 'number', array(
'label' => 'Total of Days',
'precision' => 0
))->add('diagnosis', 'textarea', array(
))->add('send', 'submit', array(
'label' => $options['lblSubmit']
));
当我使用此表单提交数据时(在我的数据库模式中,recDate
、startDate
和endDate
都是date
类型),它运行得很好。但是,如果我执行以下
public function updateLeaveAction(MedicalLeave $lc)
{
...
$form = $this->createFormBuilder(array('mleave' => $lc))
->add('mleave', 'medicalleavetype', array('lblSubmit' => 'Modify Leave'))->getForm();
...
}
有些日期被删减了,我不确定是哪一个。例如,我在数据库中存储了以下两个实例的日期(日/月/年格式):
实例1:
- 存储的实例
- 接收日期:07/04/2014
- 开始日期:2014年4月14日
- 结束日期:2014年4月20日
- 在表单中设置数据后:
- 接收日期:2014年4月6日
- 开始日期:2014年4月13日
- 结束日期:2014年4月19日
实例2:
- 存储的实例
- 接收日期:2014年4月10日
- 开始日期:2014年4月29日
- 结束日期:2014年5月6日
- 在表单中设置数据后:
- 接收日期:2014年4月9日
- 开始日期:2014年4月29日
- 结束日期:2014年5月6日
最奇怪的是,如果我检索要在网站上显示的数据,会显示正确的日期,而不是修改后的日期,所以我怀疑表格有点涉及。作为附加数据,我使用Microsoft SQL Server 2008作为我的引擎。
事先谢谢。
编辑:我刚刚在数据库内部更改了一个日期。存储的日期为2014年4月7日,我将其提前一天更改为2014年8月4日。同样的错误也发生了:现在表单显示2014年4月7日,但检索并显示在网页中的对象显示2014年8月4日。
编辑2:我已经将错误固定到transform
方法中的类Symfony'Component'Form'Extension'Core'DataTransformer'DateTimeToLocalizedStringTransformer
中。
public function transform($dateTime)
{
if (null === $dateTime) {
return '';
}
if (!$dateTime instanceof 'DateTime) {
throw new TransformationFailedException('Expected a 'DateTime.');
}
// convert time to UTC before passing it to the formatter
$dateTime = clone $dateTime;
if ('UTC' !== $this->inputTimezone) {
$dateTime->setTimezone(new 'DateTimeZone('UTC'));
}
//THE ERROR HAPPENS RIGHT IN THE LINE BELOW THIS ONE!
$value = $this->getIntlDateFormatter()->format((int) $dateTime->format('U'));
if (intl_get_error_code() != 0) {
throw new TransformationFailedException(intl_get_error_message());
}
return $value;
}
getIntlDateFormatter
返回名称所暗示的IntlDateFormatter
类,并使用以下行在同一方法中创建:
$intlDateFormatter = new 'IntlDateFormatter('Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);
所以我已经承认这是一个Intl错误。然而,我应该如何着手修复这些问题?我看到另一个问题也有Intl错误,但没有答案我的错,它有答案,但使用了create
方法,而不是__construct
。
第三版:我添加了另一个MedicalLeave
,所有日期都是从现在开始的(2004年7月27日)。所有日期都显示正确,分析日期后,从今天之前(我们结束夏令时的地方)开始的每个日期都会向后移动一天,而从现在开始的所有日期都会向前移动一天。然而,我不知道如何处理DST,尤其是使用Symfony!
我得出结论,这是PHP的错。我住在智利,我们应该在几周前改变夏令时。然而,政府认为这样做是不正确的,并将其推迟到4月27日。如果我的想法是正确的,那么从那天到4月27日的所有日期都将延迟1小时,并标记为前一天。PHP当然不了解政治,认为我们第一次约会就改变了夏令时。希望这个问题不会在好的时候再次出现。。。