Sonny
Sonny

Reputation: 8336

Zend Date Weirdness - Missing Configuration?

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

Upvotes: 0

Views: 288

Answers (4)

Derek Illchuk
Derek Illchuk

Reputation: 5658

Zend Date's getDate method is easy to misunderstand. Its output is really not to be used except to compare with another getDate output, and only to see how two dates compare vis-a-vis their calendar date. Consider it a "calendar date hash function".

Example (good): do these two dates fall on the same calendar date?

$date1->getDate()->equals($date2->getDate()); // works as expected

Example (bad):

echo $date1->getDate(); // is meaningless
echo $date1->getCalendarDateHash(); // just as this would be
$date1 = $date1->getDate(); // and don't store this meaningless value

If you're looking for it to set the time part to 00:00:00, look elsewhere.

Upvotes: 1

Sonny
Sonny

Reputation: 8336

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();
    }
}

Upvotes: 0

opHasNoName
opHasNoName

Reputation: 20736

Hum, try this:

echo $date->get(Zend_Date::DATE_FULL);

Upvotes: 0

Related Questions