Repository: briannesbitt/Carbon (PHP)

Issue:
`is` month check fails on the 31st
The `is` check for month fails when the current date is the 31st of a month.

```php
var_dump(new DateTime());                                                                                                                                                                                                               
# class DateTime#1 (3) {                                                                                                                                                                                                                        
#   public $date =>                                                                                                                                                                                                                             
#   string(26) "2025-03-31 12:00:00.000000"                                                                                                                                                                                                     
#   public $timezone_type =>                                                                                                                                                                                                                    
#   int(3)                                                                                                                                                                                                                                      
#   public $timezone =>                                                                                                                                                                                                                         
#   string(3) "UTC"                                                                                                                                                                                                                             
# }

$c = \Carbon\Carbon::parse('2019-06-02 12:23:45');
print $c;
# 2019-06-02 12:23:45
var_dump($c->is("June"));
# bool(false)
```

This is probably inherited from `DateTime`; it fails to deliver "June" when called on the 31st because June 31 doesn't exist and overflows into July:

```php
var_dump(new DateTime("June"));
# class DateTime#4 (3) {
#   public $date =>
#   string(26) "2025-07-01 00:00:00.000000"
#   public $timezone_type =>
#   int(3)
#   public $timezone =>
#   string(3) "UTC"
# }
```

Observed in Carbon version 2.72.6.

This causes the `IsTest::testIs` test to fail on the 31st.


Discussion:
I see this is related to #3032 which already had a fix for: #3033

But this case aimed to handle when the tested date is a 31st.

This fix had in turn caused the regression you spotted because it's using `Carbon::parse("June")` which like you guess use `new DateTime` and is subject to overflow.



Patch:
diff --git a/src/Carbon/Traits/Comparison.php b/src/Carbon/Traits/Comparison.php
index 6590c5a92..1b5ef99ce 100644
--- a/src/Carbon/Traits/Comparison.php
+++ b/src/Carbon/Traits/Comparison.php
@@ -1272,7 +1272,7 @@ public function is(WeekDay|Month|string $tester): bool
 
         if (preg_match('/^(?:Jan|January|Feb|February|Mar|March|Apr|April|May|Jun|June|Jul|July|Aug|August|Sep|September|Oct|October|Nov|November|Dec|December)$/i', $tester)) {
             return $this->isSameMonth(
-                $this->transmitFactory(static fn () => static::parse($tester)),
+                $this->transmitFactory(static fn () => static::parse("$tester 1st")),
                 false,
             );
         }


Test patch:
diff --git a/tests/Carbon/IsTest.php b/tests/Carbon/IsTest.php
index 7f84bccff..d296a9866 100644
--- a/tests/Carbon/IsTest.php
+++ b/tests/Carbon/IsTest.php
@@ -22,6 +22,7 @@
 use DateInterval;
 use DateTime;
 use PHPUnit\Framework\Attributes\DataProvider;
+use PHPUnit\Framework\Attributes\TestWith;
 use stdClass;
 use Tests\AbstractTestCase;
 use TypeError;
@@ -1399,8 +1400,12 @@ public function testIsCurrentFoobar()
         $date->isCurrentFoobar();
     }
 
-    public function testIs()
+    #[TestWith([null])]
+    #[TestWith(['2025-03-31 12:00'])]
+    public function testIs(?string $testNow)
     {
+        Carbon::setTestNow($testNow);
+
         $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019'));
         $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2018'));
         $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06'));
@@ -1434,6 +1439,8 @@ public function testIs()
         $this->assertTrue(Carbon::parse('2019-06-02 15:23:45')->is('3pm'));
         $this->assertFalse(Carbon::parse('2019-06-02 15:23:45')->is('4pm'));
         $this->assertFalse(Carbon::parse('2019-06-02 15:23:45')->is('3am'));
+        $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06'));
+        $this->assertTrue(Carbon::parse('2019-05-31 12:23:45')->is('2019-05'));
         $this->assertTrue(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02 12:23'));
         $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-03 12:23'));
         $this->assertFalse(Carbon::parse('2019-06-02 12:23:45')->is('2019-06-02 15:23'));