public function aggregate($from = null) { if (!is_null($from)) { $from = $this->formatDate($from); } if ($from == null) { $from = new Zend_Date(Mage::getSingleton('core/date')->gmtTimestamp(), null, Mage::app()->getLocale()->getLocaleCode()); $from->subYear(10); $this->_aggregateTickets($from->get(Varien_Date::DATETIME_INTERNAL_FORMAT)); } else { $this->_aggregateTickets($from); } $this->_refreshFlag(); return $this; }
/** * test looseBehaviour */ public function testLoose() { $date = new Zend_Date(0, 'de_DE'); try { $date->set(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } $date->set(10, 'de_DE'); $this->assertEquals(10, $date->getTimestamp()); try { $date->add(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } $date->add(10, 'de_DE'); $this->assertEquals(20, $date->getTimestamp()); try { $date->sub(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } $date->sub(10, 'de_DE'); $this->assertEquals(10, $date->getTimestamp()); try { $date->compare(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->equals(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->isEarlier(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->isLater(null, Zend_Date::YEAR); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setTime(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addTime(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subTime(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareTime(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setDate(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addDate(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subDate(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareDate(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setIso(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addIso(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subIso(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareIso(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setArpa(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addArpa(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subArpa(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareArpa(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setMonth(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addMonth(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subMonth(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareMonth(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setDay(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addDay(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subDay(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareDay(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setWeekday(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addWeekday(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subWeekday(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareWeekday(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setDayOfYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addDayOfYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subDayOfYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareDayOfYear(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setHour(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addHour(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subHour(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareHour(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setMinute(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addMinute(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subMinute(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareMinute(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setSecond(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addSecond(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subSecond(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareSecond(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->setWeek(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->addWeek(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->subWeek(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } try { $date->compareWeek(null); $this->fail(); } catch (Zend_Date_Exception $e) { // success } }
/** * 取得某个预定义时间段 * * @static * @param integer $interval * @param string $forceUnit * @param integer $timestamp * @return array */ public static function getPredefinedRange($interval, $forceUnit = null, $timestamp = null) { if (empty($timestamp)) { $timestamp = time(); } $start = new Zend_Date($timestamp); $end = new Zend_Date($timestamp); switch ($interval) { case self::TODAY: $start->setHour(0)->setMinute(0)->setSecond(0); $unit = Zend_Date::HOUR; break; case self::YESTODAY: $start->subDay(1)->setHour(0)->setMinute(0)->setSecond(0); $end = clone $start; $end->addDay(1); $unit = Zend_Date::HOUR; break; case self::TOMORROW: $start->addDay(1)->setHour(0)->setMinute(0)->setSecond(0); $end = clone $start; $end->addDay(1); $unit = Zend_Date::HOUR; break; case self::THIS_MONTH: $start->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $unit = Zend_Date::DAY; break; case self::THIS_YEAR: $start->setMonth(1)->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $end->addMonth(1)->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $unit = Zend_Date::DAY; break; case self::THIS_SEASON: $start->setMonth(3 * floor(($start->toValue('M') - 1) / 3) + 1)->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $unit = Zend_Date::DAY; case self::RECENT_24HOUR: $start->subHour(24); $unit = Zend_Date::HOUR; break; case self::RECENT_48HOUR: $start->subHour(48); $unit = Zend_Date::HOUR; break; case self::RECENT_1WEEK: $start->subWeek(1); $unit = Zend_Date::DAY; break; case self::RECENT_1MONTH: $start->subMonth(1); $unit = Zend_Date::DAY; break; case self::RECENT_24MONTH: $start->subMonth(24); $unit = Zend_Date::DAY; break; case self::LAST_1MONTH: $start->subMonth(1)->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $end = clone $start; $end->addMonth(1); $unit = Zend_Date::DAY; break; case self::LAST_1YEAR: $start->subYear(1)->setMonth(1)->setDay(1)->setHour(0)->setMinute(0)->setSecond(0); $end = clone $start; $end->addYear(1); $unit = Zend_Date::DAY; break; case self::ENTIRE_DAY: $start->setDate(self::ERA_DATE, self::ZF_DATE_FORMAT)->setTime(self::ERA_TIME, self::ZF_TIME_FORMAT); $end->addDay(1); $unit = Zend_Date::DAY; break; default: $unit = Zend_Date::SECOND; } if (!empty($forceUnit)) { $unit = $forceUnit; } $start = max(self::truncateDatetime($start, $unit), self::truncateDatetime(self::ERA_DATETIME, $unit)); $end = max(self::truncateDatetime($end, $unit), self::truncateDatetime(self::ERA_DATETIME, $unit)); return compact('start', 'end', 'unit'); }
public function getAge($atDate = null) { if ($atDate) { $zd = new Zend_Date($atDate); } else { $zd = new Zend_Date(Zend_Date::now()); } if ($this->getBirthdate()) { $from = new Zend_Date($this->getBirthdate()); return $zd->subYear($from)->get(zend_Date::YEAR); } else { return -1; } }
/** * Return interval (two GMT Zend_Date). * * @param $code * @param bool $timezone * * @return Varien_Object */ public function getInterval($code, $timezone = false) { $timestamp = Mage::getSingleton('core/date')->gmtTimestamp(); if ($timezone) { $timestamp = Mage::app()->getLocale()->date($timestamp); } $from = new Zend_Date($timestamp, null, Mage::app()->getLocale()->getLocaleCode()); $to = clone $from; $dateInterval = null; switch ($code) { case self::TODAY: $from->setTime('00:00:00'); $to->setTime('23:59:59'); break; case self::YESTERDAY: $from->subDay(1)->setTime('00:00:00'); $to->subDay(1)->setTime('23:59:59'); break; case self::THIS_MONTH: $from->setDay(1)->setTime('00:00:00'); $to->setDay(1)->addDay($to->get(Zend_Date::MONTH_DAYS) - 1)->setTime('23:59:59'); break; case self::PREVIOUS_MONTH: $from->setDay(1)->subMonth(1)->setTime('00:00:00'); $to->setDay(1)->setTime('23:59:59')->subMonth(1)->addDay($to->get(Zend_Date::MONTH_DAYS) - 1); break; case self::THIS_QUARTER: $month = intval($from->get(Zend_Date::MONTH) / 4) * 3 + 1; $from->setDay(1)->setMonth($month)->setTime('00:00:00'); $to->setDay(1)->setMonth($month)->addMonth(3)->subDay(1)->setTime('23:59:59'); break; case self::PREVIOUS_QUARTER: $month = intval($from->get(Zend_Date::MONTH) / 4) * 3 + 1; $from->setDay(1)->setMonth($month)->setTime('00:00:00')->subMonth(3); $to->setDay(1)->setMonth($month)->addMonth(3)->subDay(1)->setTime('23:59:59')->subMonth(3); break; case self::THIS_YEAR: $from->setDay(1)->setMonth(1)->setTime('00:00:00'); $to->setDay(1)->setMonth(1)->addDay($to->get(Zend_Date::LEAPYEAR) ? 365 : 364)->setTime('23:59:59'); break; case self::PREVIOUS_YEAR: $from->setDay(1)->setMonth(1)->setTime('00:00:00')->subYear(1); $to->setDay(1)->setMonth(1)->addDay($to->get(Zend_Date::LEAPYEAR) ? 365 : 364)->setTime('23:59:59')->subYear(1); break; case self::LAST_24H: $from->subDay(1); break; case self::THIS_WEEK: $weekday = $from->get(Zend_Date::WEEKDAY_DIGIT); #0-6 $from->setTime('00:00:00')->subDay($weekday); $to->setTime('23:59:59')->addDay(6 - $weekday); break; case self::PREVIOUS_WEEK: $weekday = $from->get(Zend_Date::WEEKDAY_DIGIT); #0-6 $from->setTime('00:00:00')->subDay($weekday)->subWeek(1); $to->setTime('23:59:59')->addDay(6 - $weekday)->subWeek(1); break; case self::LAST_7D: $from->subDay(7); break; case self::LAST_30D: $from->subDay(30); break; case self::LAST_3M: $from->subMonth(3); break; case self::LAST_12M: $from->subYear(1); break; case self::LIFETIME: $from->subYear(10); $to->addYear(10); break; } return new Varien_Object(array('from' => $from, 'to' => $to)); }