ZF-8308: Zend_Date comparison functions (compare, isEarlier, isLater)
Description
When using the comparison functions of Zend_Date, it does not look at timezone when comparing date datepart.
// Get FROM date.
$from = new Zend_Date();
// Start with 'Europe/Amsterdam' timezone
$from->setTimezone('Europe/Amsterdam');
// Set datetime.
$from->set('16-11-2009 00:00', Zend_Date::DATETIME);
// Get TO date.
$to = new Zend_Date();
// Start with 'Europe/Amsterdam' timezone
$to->setTimezone('Europe/Amsterdam');
// Set datetime.
$to->set('16-11-2009 00:00', Zend_Date::DATETIME);
// Change timezone to GMT.
$to->setTimezone('GMT');
Zend_Debug::dump($from->getIso(), 'FROM');
Zend_Debug::dump($to->getIso(), 'TO');
Zend_Debug::dump($to->isEarlier($from, Zend_Date::DATES), 'EARLIER');
Zend_Debug::dump($to->equals($from, Zend_Date::DATES), 'EQUALS');
Zend_Debug::dump($to->isLater($from, Zend_Date::DATES), 'LATER');
// Result in:
// FROM string(25) "2009-11-16T00:00:00+01:00"
// TO string(25) "2009-11-15T23:00:00+00:00"
// EARLIER bool(true)
// EQUALS bool(false)
// LATER bool(false)
IMHO is should be equal. We are talking about the same date, only different in timezone.
I think it should be aware of timezone. (i know, we can do it before using the function but I think it should be in the functions self!)
Sven.
Comments
Posted by Thomas Weidner (thomas) on 2009-11-16T13:01:41.000+0000
Closing as non-issue.
The used constant "DATES" is the localized date, without time. In this case wether the time nor the timezone, which is part of the time, is taken into account by the check.
When you need to validate by taking timezone into account you should use TIMESTAMP as timestamps are equal in different timezones because they are calculated according to UTC.
Posted by Sven Franke (snefit) on 2009-11-16T13:16:53.000+0000
But with "TIMESTAMP" it'll be only equal when the time-past is also equal and that is not what i tried/wanted.
I wanted to check if the date-part of a datetime was equal (taking timezone into account), but that isn't possible in this way? The only way is to set the timezones before using the compare-functions?
I thought that i was comparing the DATES-part from two given datetime (including time and timezone).
Posted by Thomas Weidner (thomas) on 2009-11-16T13:57:29.000+0000
I did not say that it's not possible.
But what you want to do is unlogic.
DATES can be for example "10.Jan.08". Or it could be "Nov.2009 10"
However DATES is specified, according to the manual, CLDR and any other documentation a DATE without time has no timestring or timezone available.
Just by looking at "10.Jan.08" you can't say if it's in GMT-1 or GMT+1.
So how should isLater() or any other comparison method take a timezone into account when there is no in the defined format.
To take a time into account, the time must be available within the format. You can not say "compare only date" and then ask "why did you not also compare time?"