Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DateTime range overlapped ?
<?php
use DatePeriod;
class DatePeriodHelper
{
/**
* @param DatePeriod $datePeriod1
* @param DatePeriod $datePeriod2
* @return boolean
*/
public static function isOverlapped(DatePeriod $datePeriod1, DatePeriod $datePeriod2)
{
return $datePeriod1->getEndDate() > $datePeriod2->getStartDate()
&& $datePeriod1->getStartDate() < $datePeriod2->getEndDate();
}
}
<?php
use DateTime;
use DatePeriod;
use DateInterval;
use Zgok\HelperBundle\Helper\DatePeriodHelper;
class DatePeriodHelperTest extends \PHPUnit_Framework_TestCase
{
/** @test */
public function isOverlapped_returns_false_when_first_dateperiod_is_before_second_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-01 01:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 02:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(false)
);
}
/** @test */
public function isOverlapped_returns_false_when_first_dateperiod_is_after_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 02:00:00');
$end = new DateTime('2016-01-01 03:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 02:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(false)
);
}
/** @test */
public function isOverlapped_returns_true_when_both_period_is_same()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 01:00:00');
$end = new DateTime('2016-01-01 02:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 02:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(true)
);
}
/** @test */
public function isOverlapped_returns_true_when_first_dateperiod_is_within_range_of_second_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 01:00:00');
$end = new DateTime('2016-01-01 02:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 00:00:00');
$secondEnd = new DateTime('2016-01-01 03:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(true)
);
}
/** @test */
public function isOverlapped_returns_true_when_first_dateperiod_start_date_is_in_scond_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 02:00:00');
$end = new DateTime('2016-01-01 04:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 03:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(true)
);
}
/** @test */
public function isOverlapped_returns_true_when_first_dateperiod_end_date_is_in_second_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-01 02:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 03:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(true)
);
}
/** @test */
public function isOverlapped_returns_true_when_second_secondperiod_is_within_first_dateperiod()
{
$interval = DateInterval::createFromDateString('1 hour');
$begin = new DateTime('2016-01-01 00:00:00');
$end = new DateTime('2016-01-01 03:00:00');
$firstDatePeriod = new DatePeriod($begin, $interval, $end);
$secondBegin = new DateTime('2016-01-01 01:00:00');
$secondEnd = new DateTime('2016-01-01 02:00:00');
$secondDatePeriod = new DatePeriod($secondBegin, $interval, $secondEnd);
assertThat(
DatePeriodHelper::isOverlapped($firstDatePeriod, $secondDatePeriod),
is(true)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.