Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,31 @@ $date = (new Jalalian(1397, 5, 24))->isPast()

```

---

```php
public function isSameAs(string $format, Jalalian|string $date): bool

$date1 = new Jalalian(1398, 6, 13);
$date2 = new Jalalian(1398, 12, 13);
$date1->isSameAs('Y-d', $date2); // true (same year and day)
$date1->isSameAs('md', $date2); // false (different month)
$date1->isSameAs('Y-m-d', '1398-06-13'); // true

```

---

```php
public function isBirthday(Jalalian|string|null $date = null): bool

$birthday = new Jalalian(1376, 6, 5);
$birthday->isBirthday('1398-06-05'); // true
$birthday->isBirthday('1398-06-06'); // false
$birthday->isBirthday(); // compares with current date

```

---
```php
public function toArray(): array
Expand Down
51 changes: 51 additions & 0 deletions src/Jalalian.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,55 @@ public function getWeekOfMonth(): int
{
return floor(($this->day + 5 - $this->getDayOfWeek()) / 7) + 1;
}

/**
* Compares the formatted values of the two dates.
*
* @example
* ```
* Jalalian::fromFormat('Y-m-d', '1398-06-13')->isSameAs('Y-d', Jalalian::fromFormat('Y-m-d', '1398-12-13')); // true
* Jalalian::fromFormat('Y-m-d', '1398-06-13')->isSameAs('Y-d', Jalalian::fromFormat('Y-m-d', '1398-06-14')); // false
* ```
*
* @param string $format date formats to compare.
* @param Jalalian|string $date instance to compare with.
*/
public function isSameAs(string $format, Jalalian|string $date): bool
{
if (is_string($date)) {
// Try to parse as Jalali date first
try {
$date = static::fromFormat('Y-m-d', $date);
} catch (\Exception $e) {
// If that fails, try as DateTime string
$date = static::fromDateTime($date);
}
}

return $this->format($format) === $date->format($format);
}

/**
* Check if its the birthday. Compares the date/month values of the two dates.
*
* @example
* ```
* Jalalian::now()->subYears(5)->isBirthday(); // true
* Jalalian::now()->subYears(5)->subDay()->isBirthday(); // false
* Jalalian::fromFormat('Y-m-d', '1398-06-05')->isBirthday(Jalalian::fromFormat('Y-m-d', '1376-06-05')); // true
* Jalalian::fromFormat('Y-m-d', '1398-06-05')->isBirthday(Jalalian::fromFormat('Y-m-d', '1376-06-06')); // false
* ```
*
* @param Jalalian|string|null $date The instance to compare with or null to use current day.
*
* @return bool
*/
public function isBirthday(Jalalian|string|null $date = null): bool
{
if ($date === null) {
$date = static::now();
}

return $this->isSameAs('md', $date);
}
}
56 changes: 56 additions & 0 deletions tests/JalalianTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,60 @@ public function testGetQuarter()
$jDate = new Jalalian(1402, 7, 23);
$this->assertEquals(3, $jDate->getQuarter());
}

public function testIsSameAs()
{
$jDate1 = new Jalalian(1398, 6, 13);
$jDate2 = new Jalalian(1398, 12, 13);
$jDate3 = new Jalalian(1398, 6, 14);
$jDate4 = new Jalalian(1374, 6, 13);

// Test same year and day
$this->assertTrue($jDate1->isSameAs('Y-d', $jDate2));

// Test different day
$this->assertFalse($jDate1->isSameAs('Y-d', $jDate3));

// Test same month and day
$this->assertTrue($jDate1->isSameAs('md', $jDate1));
$this->assertTrue($jDate1->isSameAs('md', $jDate4));

// Test with string date
$this->assertTrue($jDate1->isSameAs('Y-m-d', '1398-06-13'));
}

public function testIsBirthday()
{
// Test with same month and day (should be birthday)
$birthday = new Jalalian(1376, 6, 5);
$currentDate = new Jalalian(1398, 6, 5);
$this->assertTrue($birthday->isBirthday($currentDate));

// Test with different day (should not be birthday)
$currentDate = new Jalalian(1398, 6, 6);
$this->assertFalse($birthday->isBirthday($currentDate));

// Test with different month (should not be birthday)
$currentDate = new Jalalian(1398, 7, 5);
$this->assertFalse($birthday->isBirthday($currentDate));

// Test with string date
$this->assertTrue($birthday->isBirthday('1398-06-05'));
$this->assertFalse($birthday->isBirthday('1398-06-06'));

// Test with null (should use current date)
$now = Jalalian::now();
$sameMonthDay = new Jalalian($now->getYear() - 5, $now->getMonth(), $now->getDay());
$this->assertTrue($sameMonthDay->isBirthday());

// Test edge case: leap year
$leapYearBirthday = new Jalalian(1395, 12, 30); // leap year
$nonLeapYear = new Jalalian(1396, 12, 29); // non-leap year (max day is 29)
$this->assertFalse($leapYearBirthday->isBirthday($nonLeapYear));

// Test different years but same month/day
$birthday1 = new Jalalian(1370, 3, 15);
$birthday2 = new Jalalian(1380, 3, 15);
$this->assertTrue($birthday1->isBirthday($birthday2));
}
}
Loading