0

我从getDate()函数中得到奇怪的输出。它应该返回没有时间部分的日期,但我得到了时间部分。我是否缺少一些可以纠正此问题的配置选项?

示例代码:

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;

输出:

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

4 回答 4

1

Zend Date 的getDate方法很容易被误解。它的输出实际上不能用于与另一个getDate输出进行比较,只是为了查看两个日期如何与它们的日历日期进行比较。将其视为“日历日期哈希函数”。

示例(好):这两个日期是否属于同一日历日期?

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

示例(坏):

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

如果您正在寻找将时间部分设置为 00:00:00 的方法,请查看其他地方。

于 2011-03-10T15:36:14.937 回答
0

嗯,试试这个:

echo $date->get(Zend_Date::DATE_FULL);
于 2010-06-24T15:17:27.223 回答
0

更多见

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

于 2010-06-24T15:39:36.080 回答
0

这不是我问题的真正答案,但这是我的解决方法。我已将Zend_Date课程扩展如下:

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();
    }
}
于 2010-06-24T22:20:53.657 回答