views:

41

answers:

3

I'm getting odd output from the getDate() function. It's supposed to return a date without the time parts, but I am getting time parts. Am I missing some configuration option that would correct this?

Sample Code:

date_default_timezone_set('America/New_York');
$date = new Zend_Date(array(
    'year' => 2010,
    'month' => 3,
    'day' => 29,
));
echo $date->getIso() . PHP_EOL;
echo $date->getDate()->getIso() . PHP_EOL;

Output:

2010-03-29T00:00:00-04:00
2010-03-29T23:00:00-04:00
A: 

Hum, try this:

echo $date->get(Zend_Date::DATE_FULL);
ArneRie
That does work for output, but it doesn't return a `Zend_Date` object stripped of the time parts.
Sonny
from the manual: The getDate() method parses strings containing dates in localized formats. The results are returned in a structured array, with well-defined keys for each part of the date.
ArneRie
You're referring to `Zend_Locale_Format::getDate()`. The `getDate()` function in `Zend_Date` is documented like this: `Returns a clone of $this, with the time part set to 00:00:00.`
Sonny
A: 

For more see

http://framework.zend.com/manual/en/zend.date.constants.html#zend.date.constants.list

Ashley
My question is not about the date constants
Sonny
What version are you on? See http://zendframework.com/issues/browse/ZF-4490
Ashley
I am on 1.10.5. If you look at the last comment on that bug, another person is seeing the same behavior as me. I just wasn't sure if we both were doing the same thing wrong since there was no reply to that comment.
Sonny
Same version as you, same problem here. The only way around it is to use the set method, or revert back to the 0 timezone. Set is probably better. I'd make my own class and extend Zend_Date, that way you won't have to keep remembering to reset itEdit.. just seen you already have
Ashley
A: 

This isn't really an answer to my question, but this is my workaround. I have extended the Zend_Date class as follows:

class My_Date extends Zend_Date
{
    public static function now($locale = null)
    {
        return new My_Date(time(), self::TIMESTAMP, $locale);
    }

    /**
     * set to the first second of current day
     */
    public function setDayStart()
    {
        return $this->setHour(0)->setMinute(0)->setSecond(0);
    }

    /**
     * get the first second of current day
     */
    public function getDayStart()
    {
        $clone = clone $this;
        return $clone->setDayStart();
    }
}
Sonny