Skip to content

Instantly share code, notes, and snippets.

@shawnlindstrom
Created September 11, 2019 06:26
Show Gist options
  • Save shawnlindstrom/42aac71ff44d0b7c2cb9688a5e90f608 to your computer and use it in GitHub Desktop.
Save shawnlindstrom/42aac71ff44d0b7c2cb9688a5e90f608 to your computer and use it in GitHub Desktop.
Demonstrates That Month Addition Results in Overflow And How To Fix
<?php
namespace Tests\Unit;
use Tests\TestCase;
class CarbonNextMonthEndOfMonthTest extends TestCase
{
public function setUp(): void
{
\Carbon\Carbon::setTestNow('2019-10-31');
}
/** @test */
public function it_gets_the_wrong_last_day_of_the_end_of_the_next_month_because_of_overflow()
{
$end_of_month = \Carbon\Carbon::now()->addMonth()->endOfMonth()->format('d/m/y');
$this->assertEquals('31/12/19', $end_of_month);
}
public function it_gets_the_last_day_of_the_end_of_the_next_month()
{
$end_of_month = \Carbon\Carbon::now()->addMonthsNoOverFlow()->endOfMonth()->format('d/m/y');
$this->assertEquals('30/11/19', $end_of_month);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment