Question

Please read my third update as the problem is now about dealing with DSTs in Symfony.

I have the following form in my 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']
    ));

When I use this form for submitting data (recDate, startDate and endDate are all date type in my db schema) it works just fine. However, if I do the following

public function updateLeaveAction(MedicalLeave $lc)
{
    ...
    $form = $this->createFormBuilder(array('mleave' => $lc))
            ->add('mleave', 'medicalleavetype', array('lblSubmit' => 'Modify Leave'))->getForm();
    ...
}

some dates get substracted and I am not sure which ones. For example, I have the following dates stored in my database for two instances (day/month/year format):

Instance 1:

  • Stored instance
    • Reception date: 07/04/2014
    • Start date: 14/04/2014
    • End date: 20/04/2014
  • After setting the data in the form:
    • Reception date: 06/04/2014
    • Start date: 13/04/2014
    • End date: 19/04/2014

Instance 2:

  • Stored instance
    • Reception date: 10/04/2014
    • Start date: 29/04/2014
    • End date: 06/05/2014
  • After setting the data in the form:
    • Reception date: 09/04/2014
    • Start date: 29/04/2014
    • End date: 06/05/2014

The strangest thing is that, if I retrieve my data to be displayed in the website, the correct date is displayed rather than the modified one, so I suspect the form is somewhat involved. And as additional data I am using Microsoft SQL Server 2008 as my engine.

Thank you beforehand.

EDIT: I just changed one date internally in the database. The stored date was 07/04/2014 and I changed it one day forward, to 08/04/2014. The same error happens: now the form displays 07/04/2014 but the object when retrieved and displayed in a webpage shows 08/04/2014.

EDIT 2: I have pinned down the error to the class Symfony\Component\Form\Extension\Core\DataTransformer\DateTimeToLocalizedStringTransformer, in the transform method.

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 returns exactly what the name implies, a IntlDateFormatter class, and is created in that same method using the following line:

$intlDateFormatter = new \IntlDateFormatter(\Locale::getDefault(), $dateFormat, $timeFormat, $timezone, $calendar, $pattern);

So I've come down to the fact it's an Intl error. However, how should I go about fixing these? I saw one another question that also had errors with Intl but there were no answers my bad, it had answers but by using the create method rather than __construct.

EDIT 3: I added another MedicalLeave, with all dates in the future from now (27/04). All dates display correctly, and upon analyzing the dates, every date from before today (where we ended our DST) are moved one day backward, while all dates from now on are moved one day forward. However, I don't know how to deal with DSTs, especially using Symfony!

Was it helpful?

Solution

I came to the conclussion that it's PHP's fault. I live in Chile, and we should have changed our DST some weeks ago. However the government decided it was not correct to do so and delayed it until April 27th. If I am correct on what I think, then all dates between that day and April 27th will be delayed by 1 hour, and will be marked as the previous day. PHP, of course, doesn't know about politics, and thinks we changed our DST on the first date. Hopefully this problem won't arise again in a good time...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top