Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Feature : Add dateTimeInInterval Method #526

Merged
merged 1 commit into from
Jul 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
date($format = 'Y-m-d', $max = 'now') // '1979-06-09'
time($format = 'H:i:s', $max = 'now') // '20:49:42'
dateTimeBetween($startDate = '-30 years', $endDate = 'now') // DateTime('2003-03-15 02:00:49')
dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days') // DateTime('2003-03-15 02:00:49')
dateTimeThisCentury($max = 'now') // DateTime('1915-05-30 19:28:21')
dateTimeThisDecade($max = 'now') // DateTime('2007-05-29 22:30:48')
dateTimeThisYear($max = 'now') // DateTime('2011-02-27 20:52:14')
Expand Down
23 changes: 23 additions & 0 deletions src/Faker/Provider/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,29 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now
return $ts;
}

/**
* Get a DateTime object based on a random date between one given date and
* an interval
* Accepts date string that can be recognized by strtotime().
*
* @param string $date Defaults to 30 years ago
* @param string $interval Defaults to 5 days after
* @example dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days')
* @return \DateTime
*/
public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days')
{
$intervalObject = \DateInterval::createFromDateString($interval);
$datetime = $date instanceof \DateTime ? $date : new \DateTime($date);
$otherDatetime = clone $datetime;
$otherDatetime->add($intervalObject);

$begin = $datetime > $otherDatetime ? $otherDatetime : $datetime;
$end = $datetime===$begin ? $otherDatetime : $datetime;

return static::dateTimeBetween($begin, $end);
}

/**
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
* @example DateTime('1964-04-04 11:02:02')
Expand Down
29 changes: 29 additions & 0 deletions test/Faker/Provider/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,35 @@ public function providerDateTimeBetween()
);
}

/**
*
* @dataProvider providerDateTimeInInterval
*/
public function testDateTimeInInterval($start, $interval = "+5 days", $isInFutur)
{
$date = DateTimeProvider::dateTimeInInterval($start, $interval);
$this->assertInstanceOf('\DateTime', $date);

$_interval = \DateInterval::createFromDateString($interval);
$_start = new \DateTime($start);
if($isInFutur){
$this->assertGreaterThanOrEqual($_start, $date);
$this->assertLessThanOrEqual($_start->add($_interval), $date);
}else{
$this->assertLessThanOrEqual($_start, $date);
$this->assertGreaterThanOrEqual($_start->add($_interval), $date);
}
}

public function providerDateTimeInInterval()
{
return array(
array('-1 year', '+5 days', true),
array('-1 day', '-1 hour', false),
array('-1 day', '+1 hour', true),
);
}

public function testFixedSeedWithMaximumTimestamp()
{
$max = '2018-03-01 12:00:00';
Expand Down