Issues

ZF-2587: Issues with calculate dates

Description

When i try to calculate dates with the Zend_Date->set() method, i get wrong values.


date_default_timezone_set('UTC');

$date = new Zend_Date(mktime(1, 1, 1, 1, 22, 2008));
$date->set(
    array(
        'day' => $date->get(Zend_Date::DAY),
        'month' => $date->get(Zend_Date::MONTH),
        'year' => $date->get(Zend_Date::YEAR),
        'hour' => 23,
        'minute' => 59,
        'second' => 59
    )
);

$date->add(6, Zend_Date::MONTH);
$date->setTimezone('Europe/Berlin');
echo $date->get(Zend_Date::DATE_MEDIUM) . ' ' . $date->get(Zend_Date::TIME_MEDIUM);  

Example 1 works good. The result is 23.07.2008 01:59:59.


date_default_timezone_set('UTC');

$date = new Zend_Date(mktime(1, 1, 1, 1, 22, 2008));
$date->setTimezone('Europe/Berlin');
$date->set(
    array(
        'day' => $date->get(Zend_Date::DAY),
        'month' => $date->get(Zend_Date::MONTH),
        'year' => $date->get(Zend_Date::YEAR),
        'hour' => 23,
        'minute' => 59,
        'second' => 59
    )
);

$date->add(6, Zend_Date::MONTH);
echo $date->get(Zend_Date::DATE_MEDIUM) . ' ' . $date->get(Zend_Date::TIME_MEDIUM);  

Example 2: When i set the timezone first the result is 02.07.2008 00:59:59.


date_default_timezone_set('UTC');

$date = new Zend_Date(mktime(1, 1, 1, 1, 22, 2008));
$date->set(
    array(
        'day' => $date->get(Zend_Date::DAY),
        'month' => $date->get(Zend_Date::MONTH),
        'year' => $date->get(Zend_Date::YEAR),
        'hour' => 23,
        'minute' => 59,
        'second' => 59
    )
);
$date->setTimezone('Europe/Berlin');
$date->add(6, Zend_Date::MONTH);
echo $date->get(Zend_Date::DATE_MEDIUM) . ' ' . $date->get(Zend_Date::TIME_MEDIUM);  

Example 3: When i set the timezone before i add the 6 months the result is 01.07.2008 01:59:59.

Comments

In your case you should set the option "extended_month" to true. This option handles how months are calculated.

Sorry, there was an typo.... this is an working example... it has to be set before working with date (set/add/sub)


Zend_Date::setOptions(array('extend_month' => true));

Hello Thomas

The code works almost. Example 1 and Example 3 are correct. Example 2 is one hour in the past. Is there anything to note?

I have found an other issue with the add method.


date_default_timezone_set('UTC');

$date = new Zend_Date();
$date->set(30, Zend_Date::DAY);
$date->set(7, Zend_Date::MONTH);
$date->set(2008, Zend_Date::YEAR);
$date->set(0, Zend_Date::HOUR);
$date->set(0, Zend_Date::MINUTE);
$date->set(0, Zend_Date::SECOND);
echo $date->get(Zend_Date::DATE_MEDIUM) . ' ' . $date->get(Zend_Date::TIME_MEDIUM);

This example displays 01.07.2008 00:00:00. When is set 29 as day it works correct.

Best regards Christian

Your problem is that UTC does not have summer/wintertime... So you set CET... and then you set a date... now summer/wintertime calculation is done, the resulting date has set +1... And THEN you add 6 months... now we have +2 (summertime)... but the difference from +1 to +2 is only one hour and not 2 hours.

This is the difference you have seen. Please note that date-calculation differs is you use timezones or UTC/GMT.

You second problem is also simple: We have February...

You are setting the actual date with new Zend_Date()... Then you try to set the 30.February... but there is no 30.February... by default Zend_Date corrects dates automatically so what you get is 1.March... Normally you would set values from greatest to smallest... so you have to do Year, Month, Day, Hour, Minute, Second... then you will have no problem, or you use array, or you give the date as string... there are several ways.

Ok. This is blausible.

Thanks a lot!