public static function getPreviousPeriod(PeriodData $period) { $previousPeriod = new \StdClass(); $periodFrom = new \DateTimeImmutable($period->getFrom()); $previousPeriod->from = $periodFrom->sub(new \DateInterval("P1M"))->format("Y-m-d"); $previousPeriod->to = $periodFrom->sub(new \DateInterval("P1D"))->format("Y-m-d"); return $previousPeriod; }
protected function createRequestForSeconds($seconds) { $interval = new \DateInterval('PT' . abs($seconds) . 'S'); $date = new \DateTimeImmutable('2015-01-01 00:00:00'); if ($seconds < 0) { $baseDate = $date->add($interval); } else { $baseDate = $date->sub($interval); } return new DateDiffRequest($date, $baseDate); }
public function testSub() { $time = '2000-01-02T03:14:25'; $immutable = new DateTimeImmutable($time); $control = new DateTimeImmutable($time); $mutable = new DateTime($time); $interval = new DateInterval('P1D'); $new = $immutable->sub($interval); $mutable->sub($interval); $this->assertNotSame($immutable, $new); $this->assertSame($control->format(DateTime::RFC3339), $immutable->format(DateTime::RFC3339)); $this->assertSame($mutable->format(DateTime::RFC3339), $new->format(DateTime::RFC3339)); }
/** * @param \DateInterval $interval * @return Price[] */ private function findPricesForInterval(\DateInterval $interval) { $today = new \DateTimeImmutable('today'); $forDate = $today->sub($interval); $sql = sprintf('SELECT * FROM %s p WHERE p.date_time > ? AND p.date_time <= ? ORDER BY p.date_time ASC ', $this->getTableName()); /** @var Statement $statement */ $statement = $this->connection->prepare($sql); $statement->bindValue(1, $forDate, Type::DATETIME); $statement->bindValue(2, $today, Type::DATETIME); $statement->execute(); $results = $statement->fetchAll(); if (empty($results)) { return null; } $objects = []; foreach ($results as $result) { $objects[] = $this->createEntity($result); } return $objects; }
public function testWillNotAddBidOnEndedAuction() { $bidPrice = new Money(2, new Currency('EUR')); $now = new DateTimeImmutable(); $startTime = $now->sub(new DateInterval("PT4H")); $endTime = $now->sub(new DateInterval("PT2H")); $auction = $this->createAuction($this->title, $this->description, $this->startPrice, $this->owner, $startTime, $endTime); $this->setExpectedException(LogicException::class, "You may not bid on a ended auction"); $auction->addBid($bidPrice, $this->bidder); }
/** * @test */ public function scheduleAtWithPassedTimeSchedulesEventImmediately() { $eventBus = $this->createEventBus(); $event = $this->getMockBuilder(Event::class)->getMock(); $eventBus->expects(self::once())->method('publish')->with($event); // schedule for execution 1 day ago $now = new \DateTimeImmutable(); $time = $now->sub(new \DateInterval('P1D')); $scheduler = new SimpleEventScheduler($eventBus); $scheduler->scheduleAt($time, $event); $this->wait(1); }
<?php $date = new DateTimeImmutable(); $pd = $date->sub(new DateInterval('P2M')); $nd = $date->add(new DateInterval('P2Y')); echo $pd->format('Y-m-d') . "\n"; echo $nd->format('Y-m-d') . "\n";
/** * Factory method to construct an instance which retrieves entries from * a default period of time ago. */ public static function createByDate() { $date = new DateTimeImmutable(); $from_date = $date->sub(new DateInterval('P' . NMONTHS . 'M')); $h = curl_init(ALBO_CT_URL); if (!$h) { throw new Exception("Unable to initialize cURL session"); } curl_setopt($h, CURLOPT_POST, TRUE); curl_setopt($h, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($h, CURLOPT_POSTFIELDS, array("__Click" => 0, "%%Surrogate_gg1" => 1, "gg1" => $from_date->format('d'), "%%Surrogate_mm1" => 1, "mm1" => $from_date->format('m'), "%%Surrogate_aa1" => 1, "aa1" => $from_date->format('Y'))); //curl_setopt($h, CURLOPT_HTTPHEADER, array("Accept-Charset: utf-8")); $page = curl_exec($h); if ($page == FALSE) { throw new Exception("Unable to execute POST request: " . curl_error($h)); } curl_close($h); return new AlboComuneCTParser($page); }