public function testCreateForMonth() { $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 10:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 15:30:45'); $datePeriodFromArray = $datePeriodToArray = array(); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-13 15:30:45'); $datePeriodFromArray = $datePeriodToArray = array(); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-10-10 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-15 10:30:45'); $datePeriodFromArray = $datePeriodToArray = array(); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-09-10 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-15 10:30:45'); $datePeriodFromArray = $datePeriodToArray = array('2012-10-01 00:00:00'); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2012-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodFromArray = $datePeriodToArray = array('2012-09-01 00:00:00', '2012-10-01 00:00:00'); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); $from = DateTime::createFromFormat('Y-m-d H:i:s', '2010-08-20 15:30:45'); $to = DateTime::createFromFormat('Y-m-d H:i:s', '2012-11-10 10:30:45'); $datePeriodFromArray = array('2010-09-01 00:00:00', '2010-10-01 00:00:00', '2010-11-01 00:00:00', '2010-12-01 00:00:00'); $datePeriodToArray = array('2012-01-01 00:00:00', '2012-02-01 00:00:00', '2012-03-01 00:00:00', '2012-04-01 00:00:00', '2012-05-01 00:00:00', '2012-06-01 00:00:00', '2012-07-01 00:00:00', '2012-08-01 00:00:00', '2012-09-01 00:00:00', '2012-10-01 00:00:00'); $this->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_FROM))->array($datePeriod->toArray(true))->isEqualTo($datePeriodFromArray)->if($datePeriod = TestedDatePeriod::createForMonth($from, $to, TestedDatePeriod::CREATE_TO))->array($datePeriod->toArray(true))->isEqualTo($datePeriodToArray); }
public function bitDateRange($key, $destKey, DateTime $from, DateTime $to) { if ($from > $to) { throw new Exception("DateTime from (" . $from->format('Y-m-d H:i:s') . ") must be anterior to DateTime to (" . $to->format('Y-m-d H:i:s') . ")."); } $from = clone $from; $to = clone $to; $this->getRedisClient()->del($this->prefixTempKey . $destKey); // Minutes $minutesFrom = DatePeriod::createForMinute($from, $to, DatePeriod::CREATE_FROM); foreach ($minutesFrom as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } $minutesTo = DatePeriod::createForMinute($from, $to, DatePeriod::CREATE_TO); if (array_diff($minutesTo->toArray(true), $minutesFrom->toArray(true)) !== array_diff($minutesFrom->toArray(true), $minutesTo->toArray(true))) { foreach ($minutesTo as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } } // Hours $hoursFrom = DatePeriod::createForHour($from, $to, DatePeriod::CREATE_FROM); foreach ($hoursFrom as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } $hoursTo = DatePeriod::createForHour($from, $to, DatePeriod::CREATE_TO); if (array_diff($hoursTo->toArray(true), $hoursFrom->toArray(true)) !== array_diff($hoursFrom->toArray(true), $hoursTo->toArray(true))) { foreach ($hoursTo as $date) { $this->bitOpOr($destKey, new Hour($key, $date), $destKey); } } // Days $daysFrom = DatePeriod::createForDay($from, $to, DatePeriod::CREATE_FROM); foreach ($daysFrom as $date) { $this->bitOpOr($destKey, new Day($key, $date), $destKey); } $daysTo = DatePeriod::createForDay($from, $to, DatePeriod::CREATE_TO); if (array_diff($daysTo->toArray(true), $daysFrom->toArray(true)) !== array_diff($daysFrom->toArray(true), $daysTo->toArray(true))) { foreach ($daysTo as $date) { $this->bitOpOr($destKey, new Day($key, $date), $destKey); } } // Months $monthsFrom = DatePeriod::createForMonth($from, $to, DatePeriod::CREATE_FROM); foreach ($monthsFrom as $date) { $this->bitOpOr($destKey, new Month($key, $date), $destKey); } $monthsTo = DatePeriod::createForMonth($from, $to, DatePeriod::CREATE_TO); if (array_diff($monthsTo->toArray(true), $monthsFrom->toArray(true)) !== array_diff($monthsFrom->toArray(true), $monthsTo->toArray(true))) { foreach ($monthsTo as $date) { $this->bitOpOr($destKey, new Month($key, $date), $destKey); } } // Years $years = DatePeriod::createForYear($from, $to); foreach ($years as $date) { $this->bitOpOr($destKey, new Year($key, $date), $destKey); } $this->getRedisClient()->sadd($this->prefixTempKey . 'keys', $destKey); $this->getRedisClient()->expire($destKey, $this->expireTimeout); return $this; }
/** * Return a DatePeriod instance from the provided options. * * @param mixed $from * @param mixed $to * @param string $fromOrTo * @return \FreeAgent\Bitter\Date\DatePeriod */ public function periodForMonth(DateTime $from, DateTime $to, $fromOrTo) { $from = $from instanceof DateTime ? $from : new Carbon($from); $to = $to instanceof DateTime ? $to : new Carbon($to); return DatePeriod::createForMonth($from, $to, $fromOrTo); }