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

Commit 42bef72

Browse files
committed
Merge pull request #526 from nicodmf/patch-1
Feature : Add dateTimeInInterval Method
2 parents 09bb428 + 7d2e376 commit 42bef72

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
186186
date($format = 'Y-m-d', $max = 'now') // '1979-06-09'
187187
time($format = 'H:i:s', $max = 'now') // '20:49:42'
188188
dateTimeBetween($startDate = '-30 years', $endDate = 'now') // DateTime('2003-03-15 02:00:49')
189+
dateTimeInInterval($startDate = '-30 years', $interval = '+ 5 days') // DateTime('2003-03-15 02:00:49')
189190
dateTimeThisCentury($max = 'now') // DateTime('1915-05-30 19:28:21')
190191
dateTimeThisDecade($max = 'now') // DateTime('2007-05-29 22:30:48')
191192
dateTimeThisYear($max = 'now') // DateTime('2011-02-27 20:52:14')

src/Faker/Provider/DateTime.php

+23
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,29 @@ public static function dateTimeBetween($startDate = '-30 years', $endDate = 'now
120120
return $ts;
121121
}
122122

123+
/**
124+
* Get a DateTime object based on a random date between one given date and
125+
* an interval
126+
* Accepts date string that can be recognized by strtotime().
127+
*
128+
* @param string $date Defaults to 30 years ago
129+
* @param string $interval Defaults to 5 days after
130+
* @example dateTimeInInterval('1999-02-02 11:42:52', '+ 5 days')
131+
* @return \DateTime
132+
*/
133+
public static function dateTimeInInterval($date = '-30 years', $interval = '+5 days')
134+
{
135+
$intervalObject = \DateInterval::createFromDateString($interval);
136+
$datetime = $date instanceof \DateTime ? $date : new \DateTime($date);
137+
$otherDatetime = clone $datetime;
138+
$otherDatetime->add($intervalObject);
139+
140+
$begin = $datetime > $otherDatetime ? $otherDatetime : $datetime;
141+
$end = $datetime===$begin ? $otherDatetime : $datetime;
142+
143+
return static::dateTimeBetween($begin, $end);
144+
}
145+
123146
/**
124147
* @param \DateTime|int|string $max maximum timestamp used as random end limit, default to "now"
125148
* @example DateTime('1964-04-04 11:02:02')

test/Faker/Provider/DateTimeTest.php

+29
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,35 @@ public function providerDateTimeBetween()
7474
);
7575
}
7676

77+
/**
78+
*
79+
* @dataProvider providerDateTimeInInterval
80+
*/
81+
public function testDateTimeInInterval($start, $interval = "+5 days", $isInFutur)
82+
{
83+
$date = DateTimeProvider::dateTimeInInterval($start, $interval);
84+
$this->assertInstanceOf('\DateTime', $date);
85+
86+
$_interval = \DateInterval::createFromDateString($interval);
87+
$_start = new \DateTime($start);
88+
if($isInFutur){
89+
$this->assertGreaterThanOrEqual($_start, $date);
90+
$this->assertLessThanOrEqual($_start->add($_interval), $date);
91+
}else{
92+
$this->assertLessThanOrEqual($_start, $date);
93+
$this->assertGreaterThanOrEqual($_start->add($_interval), $date);
94+
}
95+
}
96+
97+
public function providerDateTimeInInterval()
98+
{
99+
return array(
100+
array('-1 year', '+5 days', true),
101+
array('-1 day', '-1 hour', false),
102+
array('-1 day', '+1 hour', true),
103+
);
104+
}
105+
77106
public function testFixedSeedWithMaximumTimestamp()
78107
{
79108
$max = '2018-03-01 12:00:00';

0 commit comments

Comments
 (0)