Skip to content

Commit

Permalink
DEV: DateTimeHelper difference in days will also return negative values
Browse files Browse the repository at this point in the history
  • Loading branch information
TonisOrmisson committed Aug 30, 2018
1 parent 8a3bc22 commit b517ca4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/DateHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ public function getDateDifferenceInDays($sqlDate) {
$dStart = $this->now;
$dEnd = new DateTime($sqlDate);
$dDiff = $dStart->diff($dEnd);
$days = $dDiff->days;
return $days;
return (int) $dDiff->format('%r%a');
}

/**
Expand Down
17 changes: 12 additions & 5 deletions tests/unit/DateHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace andmemasin\helpers;

use andmemasin\helpers\DateHelper;
use Codeception\Stub;

class DateHelperTest extends \Codeception\Test\Unit
Expand Down Expand Up @@ -83,11 +82,19 @@ public function testGetDatetimeForDisplay() {

public function provideFromTo() {
return [
["2018-01-01", 364],
["2018-12-01", 30],
// past
["2018-01-01", -364],
["2018-12-01", -30],
// with time
["2018-12-01 00:00:00", 30],
["2018-12-30", 1],
["2018-12-01 00:00:00", -30],

["2018-12-30", -1],

// future
["2019-01-01", 1],
["2019-01-30", 31],
["2019-12-31", 365],

];
}

Expand Down

0 comments on commit b517ca4

Please sign in to comment.