From 8af500904d35fa5fa177de40e81c9bf66f34668d Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Sat, 18 Oct 2025 10:47:25 +0330 Subject: [PATCH 1/2] [feat] add method isBirthday and isSameAs --- src/Jalalian.php | 51 ++++++++++++++++++++++++++++++++++++++ tests/JalalianTest.php | 56 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/src/Jalalian.php b/src/Jalalian.php index 3fc5788..f456f97 100644 --- a/src/Jalalian.php +++ b/src/Jalalian.php @@ -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); + } } diff --git a/tests/JalalianTest.php b/tests/JalalianTest.php index b22ca09..9885a3c 100644 --- a/tests/JalalianTest.php +++ b/tests/JalalianTest.php @@ -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)); + } } From 12dcb72a21a515e80376ac573dc21ee24b50bf17 Mon Sep 17 00:00:00 2001 From: Ali Sasani Date: Sat, 18 Oct 2025 10:50:58 +0330 Subject: [PATCH 2/2] [doc] add document for isSameAs and isBirthday --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index f5f9415..9edb863 100644 --- a/README.md +++ b/README.md @@ -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