From 3724b279f8c0309fe540054ecdd7f08c7cc09475 Mon Sep 17 00:00:00 2001 From: Lucas Date: Tue, 26 Nov 2013 22:32:20 +0100 Subject: [PATCH 1/3] Add startOfYear() and endOfYear() functions --- readme.src.md | 6 ++++++ src/Carbon/Carbon.php | 20 +++++++++++++++++++ tests/StartEndOfTest.php | 42 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/readme.src.md b/readme.src.md index 827e9afdb8..7a129a528d 100644 --- a/readme.src.md +++ b/readme.src.md @@ -711,6 +711,12 @@ These group of methods perform helpful modifications to the current instance. M {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} {{modifier4::exec(echo $dt->endOfMonth();/*pad(50)*/)}} // {{modifier4_eval}} +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier15::exec(echo $dt->startOfYear();/*pad(50)*/)}} // {{modifier15_eval}} + +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier16::exec(echo $dt->endOfYear();/*pad(50)*/)}} // {{modifier16_eval}} + {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} {{modifier5::exec(echo $dt->startOfWeek();/*pad(50)*/)}} // {{modifier5_eval}} {{modifier6::exec(var_dump($dt->dayOfWeek == Carbon::MONDAY);/*pad(50)*/)}} // {{modifier6_eval}} : ISO8601 week starts on Monday diff --git a/src/Carbon/Carbon.php b/src/Carbon/Carbon.php index 72fe98d705..cc3f7bb7c5 100644 --- a/src/Carbon/Carbon.php +++ b/src/Carbon/Carbon.php @@ -1766,6 +1766,26 @@ public function endOfMonth() return $this->day($this->daysInMonth)->endOfDay(); } + /** + * Resets the date to the first day of the year and the time to 00:00:00 + * + * @return Carbon + */ + public function startOfYear() + { + return $this->month(1)->startOfMonth(); + } + + /** + * Resets the date to end of the year and time to 23:59:59 + * + * @return Carbon + */ + public function endOfYear() + { + return $this->month(12)->endOfMonth(); + } + /** * Resets the date to the first day of the ISO-8601 week (Monday) and the time to 00:00:00 * diff --git a/tests/StartEndOfTest.php b/tests/StartEndOfTest.php index e64270c8a2..6d49186187 100644 --- a/tests/StartEndOfTest.php +++ b/tests/StartEndOfTest.php @@ -42,6 +42,27 @@ public function testStartOfMonthFromLastDay() $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); } + public function testStartOfYearIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->startOfYear() instanceof Carbon); + } + public function testStartOfYearFromNow() + { + $dt = Carbon::now()->startOfYear(); + $this->assertCarbon($dt, $dt->year, 1, 1, 0, 0, 0); + } + public function testStartOfYearFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfYear(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + public function testStartOfYearFromLastDay() + { + $dt = Carbon::create(2000, 12, 31, 23, 59, 59)->startOfYear(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + public function testEndOfMonthIsFluid() { $dt = Carbon::now(); @@ -57,4 +78,25 @@ public function testEndOfMonthFromLastDay() $dt = Carbon::create(2000, 1, 31, 2, 3, 4)->endOfMonth(); $this->assertCarbon($dt, 2000, 1, 31, 23, 59, 59); } + + public function testEndOfYearIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->endOfYear() instanceof Carbon); + } + public function testEndOfYearFromNow() + { + $dt = Carbon::now()->endOfYear(); + $this->assertCarbon($dt, $dt->year, 12, 31, 23, 59, 59); + } + public function testEndOfYearFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfYear(); + $this->assertCarbon($dt, 2000, 12, 31, 23, 59, 59); + } + public function testEndOfYearFromLastDay() + { + $dt = Carbon::create(2000, 12, 31, 23, 59, 59)->endOfYear(); + $this->assertCarbon($dt, 2000, 12, 31, 23, 59, 59); + } } From 91cd9b586337c54f30781c14faf12d188e57197f Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 27 Nov 2013 13:00:47 +0100 Subject: [PATCH 2/3] Add start/end of year/decace/century functions Close and replace PR https://github.com/briannesbitt/Carbon/pull/63 --- src/Carbon/Carbon.php | 41 ++++++++++++++++++++ tests/StartEndOfTest.php | 84 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/src/Carbon/Carbon.php b/src/Carbon/Carbon.php index cc3f7bb7c5..911eef1b8e 100644 --- a/src/Carbon/Carbon.php +++ b/src/Carbon/Carbon.php @@ -1786,6 +1786,47 @@ public function endOfYear() return $this->month(12)->endOfMonth(); } + /** + * Resets the date to the first day of the decade and the time to 00:00:00 + * + * @return Carbon + */ + public function startOfDecade() + { + return $this->startOfYear()->year($this->year - $this->year % 10); + } + + /** + * Resets the date to end of the decade and time to 23:59:59 + * + * @return Carbon + */ + public function endOfDecade() + { + return $this->endOfYear()->year($this->year - $this->year % 10 + 9); + } + + + /** + * Resets the date to the first day of the century and the time to 00:00:00 + * + * @return Carbon + */ + public function startOfCentury() + { + return $this->startOfYear()->year($this->year - $this->year % 100); + } + + /** + * Resets the date to end of the century and time to 23:59:59 + * + * @return Carbon + */ + public function endOfCentury() + { + return $this->endOfYear()->year($this->year - $this->year % 100 + 99); + } + /** * Resets the date to the first day of the ISO-8601 week (Monday) and the time to 00:00:00 * diff --git a/tests/StartEndOfTest.php b/tests/StartEndOfTest.php index 6d49186187..f9fd9e7e97 100644 --- a/tests/StartEndOfTest.php +++ b/tests/StartEndOfTest.php @@ -99,4 +99,88 @@ public function testEndOfYearFromLastDay() $dt = Carbon::create(2000, 12, 31, 23, 59, 59)->endOfYear(); $this->assertCarbon($dt, 2000, 12, 31, 23, 59, 59); } + + public function testStartOfDecadeIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->startOfDecade() instanceof Carbon); + } + public function testStartOfDecadeFromNow() + { + $dt = Carbon::now()->startOfDecade(); + $this->assertCarbon($dt, $dt->year - $dt->year % 10, 1, 1, 0, 0, 0); + } + public function testStartOfDecadeFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfDecade(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + public function testStartOfDecadeFromLastDay() + { + $dt = Carbon::create(2009, 12, 31, 23, 59, 59)->startOfDecade(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + + public function testEndOfDecadeIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->endOfDecade() instanceof Carbon); + } + public function testEndOfDecadeFromNow() + { + $dt = Carbon::now()->endOfDecade(); + $this->assertCarbon($dt, $dt->year - $dt->year % 10 + 9, 12, 31, 23, 59, 59); + } + public function testEndOfDecadeFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfDecade(); + $this->assertCarbon($dt, 2009, 12, 31, 23, 59, 59); + } + public function testEndOfDecadeFromLastDay() + { + $dt = Carbon::create(2009, 12, 31, 23, 59, 59)->endOfDecade(); + $this->assertCarbon($dt, 2009, 12, 31, 23, 59, 59); + } + + public function testStartOfCenturyIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->startOfCentury() instanceof Carbon); + } + public function testStartOfCenturyFromNow() + { + $dt = Carbon::now()->startOfCentury(); + $this->assertCarbon($dt, $dt->year - $dt->year % 100, 1, 1, 0, 0, 0); + } + public function testStartOfCenturyFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->startOfCentury(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + public function testStartOfCenturyFromLastDay() + { + $dt = Carbon::create(2009, 12, 31, 23, 59, 59)->startOfCentury(); + $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); + } + + public function testEndOfCenturyIsFluid() + { + $dt = Carbon::now(); + $this->assertTrue($dt->endOfCentury() instanceof Carbon); + } + public function testEndOfCenturyFromNow() + { + $dt = Carbon::now()->endOfCentury(); + $this->assertCarbon($dt, $dt->year - $dt->year % 100 + 99, 12, 31, 23, 59, 59); + } + public function testEndOfCenturyFromFirstDay() + { + $dt = Carbon::create(2000, 1, 1, 1, 1, 1)->endOfCentury(); + $this->assertCarbon($dt, 2099, 12, 31, 23, 59, 59); + } + public function testEndOfCenturyFromLastDay() + { + $dt = Carbon::create(2099, 12, 31, 23, 59, 59)->endOfCentury(); + $this->assertCarbon($dt, 2099, 12, 31, 23, 59, 59); + } } From 0d6692ae9db0cf2316572517564881d9185be409 Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 27 Nov 2013 13:04:00 +0100 Subject: [PATCH 3/3] Also update readme --- readme.src.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/readme.src.md b/readme.src.md index 7a129a528d..b6fa66d1ab 100644 --- a/readme.src.md +++ b/readme.src.md @@ -717,6 +717,18 @@ These group of methods perform helpful modifications to the current instance. M {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} {{modifier16::exec(echo $dt->endOfYear();/*pad(50)*/)}} // {{modifier16_eval}} +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier17::exec(echo $dt->startOfDecade();/*pad(50)*/)}} // {{modifier17_eval}} + +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier18::exec(echo $dt->endOfDecade();/*pad(50)*/)}} // {{modifier18_eval}} + +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier19::exec(echo $dt->startOfCentury();/*pad(50)*/)}} // {{modifier19_eval}} + +{{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} +{{modifier20::exec(echo $dt->endOfCentury();/*pad(50)*/)}} // {{modifier20_eval}} + {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} {{modifier5::exec(echo $dt->startOfWeek();/*pad(50)*/)}} // {{modifier5_eval}} {{modifier6::exec(var_dump($dt->dayOfWeek == Carbon::MONDAY);/*pad(50)*/)}} // {{modifier6_eval}} : ISO8601 week starts on Monday