开发者

Zend Date Weirdness - Missing Configuration?

开发者 https://www.devze.com 2023-01-05 02:30 出处:网络
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?

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(开发者_如何学Pythonarray(
    '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


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.


Hum, try this:

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


For more see

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号