Skip to content

Commit

Permalink
Merge pull request #157 from majidalaeinia/master
Browse files Browse the repository at this point in the history
Add New Methods: `subDay`, `getLastWeek`, `addDay`, `getLastMonth`
  • Loading branch information
morilog committed Mar 22, 2023
2 parents c0e5ec8 + ba41fa3 commit 1cefc64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Jalalian.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,11 @@ public function subDays(int $days = 1): Jalalian
return static::fromCarbon($this->toCarbon()->subDays($days));
}

public function subDay(): Jalalian
{
return $this->subDays(1);
}

/**
* @return Carbon
*/
Expand Down Expand Up @@ -648,16 +653,31 @@ public function getNextWeek(): Jalalian
return $this->addDays(7);
}

public function getLastWeek(): Jalalian
{
return $this->subDays(7);
}

public function addDays(int $days = 1): Jalalian
{
return static::fromCarbon($this->toCarbon()->addDays($days));
}

public function addDay(): Jalalian
{
return $this->addDays(1);
}

public function getNextMonth(): Jalalian
{
return $this->addMonths(1);
}

public function getLastMonth(): Jalalian
{
return $this->subMonths(1);
}

public function getWeekOfMonth(): int
{
return floor(($this->day + 5 - $this->getDayOfWeek()) / 7) + 1;
Expand Down
24 changes: 24 additions & 0 deletions tests/JalalianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,28 @@ public function testGetFirstDayOfYear()
$jDate = new Jalalian(1393, 9, 5);
$this->assertEquals($jDate->getFirstDayOfYear()->format('Y-m-d'), '1393-01-01');
}

public function testAddDay()
{
$jDate = new Jalalian(1401, 6, 31);
$this->assertEquals($jDate->addDay()->format('Y-m-d'), '1401-07-01');
}

public function testSubDay()
{
$jDate = new Jalalian(1401, 6, 1);
$this->assertEquals($jDate->subDay()->format('Y-m-d'), '1401-05-31');
}

public function testGetLastWeek()
{
$jDate = new Jalalian(1401, 6, 8);
$this->assertEquals($jDate->getLastWeek()->format('Y-m-d'), '1401-06-01');
}

public function testGetLastMonth()
{
$jDate = new Jalalian(1401, 6, 8);
$this->assertEquals($jDate->getLastMonth()->format('Y-m-d'), '1401-05-08');
}
}

0 comments on commit 1cefc64

Please sign in to comment.