Skip to content

Commit

Permalink
Date Time Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 16, 2017
1 parent 3c13584 commit 2356a28
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/DateTimeHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
namespace Packaged\Helpers;

class DateTimeHelper
{
/**
* @param string $inputDates e.g. "2017-01-02-2017-01-05,2017-03-01-04-01"
*
* @return array array of dates
*/
public static function dateRange($inputDates)
{
$finalDates = [];
$dates = Strings::stringToRange($inputDates);
foreach($dates as $date)
{
if(strlen($date) > 10)
{
$dp = explode('-', $date);
$firstDate = implode('-', array_slice($dp, 0, 3));
$secondDate = null;
$sdp = array_slice($dp, 3);
if(!empty(implode("", $sdp)))
{
switch(count($sdp))
{
case 1:
$secondDate = implode('-', [$dp[0], $dp[1], $sdp[0]]);
break;
case 2:
$secondDate = implode('-', [$dp[0], $sdp[0], $sdp[1]]);
break;
case 3:
$secondDate = implode('-', [$sdp[0], $sdp[1], $sdp[2]]);
break;
}
}
if($secondDate == null)
{
$finalDates[] = $firstDate;
}
else
{
$firstTime = strtotime($firstDate);
$secondTime = strtotime($secondDate);

if($firstTime > $secondTime)
{
$tmp = $secondTime;
$secondTime = $firstTime;
$firstTime = $tmp;
}

if($firstTime > 0 && $secondTime > 0)
{
$currentTime = $firstTime;
while($currentTime <= $secondTime)
{
$finalDates[] = date("Y-m-d", $currentTime);
$currentTime += 86400;
}
}
}
}
else
{
$finalDates[] = $date;
}
}
return $finalDates;
}
}
64 changes: 64 additions & 0 deletions tests/DateTimeHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

class DateTimeHelperTest extends PHPUnit_Framework_TestCase
{
public function testDate()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-01"
);
$this->assertEquals(["2017-01-01"], $dates);
}

public function testBasicDates()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-01,2017-02-02"
);
$this->assertEquals(["2017-01-01", "2017-02-02"], $dates);
}

public function testSameMonthRange()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-01-03"
);
$this->assertEquals(["2017-01-01", "2017-01-02", "2017-01-03"], $dates);
}

public function testSameYearRange()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-30-02-01"
);
$this->assertEquals(["2017-01-30", "2017-01-31", "2017-02-01"], $dates);
}

public function testRange()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-25-2017-01-29"
);
$this->assertEquals(
["2017-01-25", "2017-01-26", "2017-01-27", "2017-01-28", "2017-01-29"],
$dates
);
}

public function testInverseDateRange()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-03-01"
);
$this->assertEquals(["2017-01-01", "2017-01-02", "2017-01-03"], $dates);
}

public function testInvalidDateRange()
{
$dates = \Packaged\Helpers\DateTimeHelper::dateRange(
"2017-01-03-"
);
$this->assertEquals(["2017-01-03"], $dates);
}

}

0 comments on commit 2356a28

Please sign in to comment.