├── .gitignore ├── .travis.yml ├── composer.json ├── phpunit.xml.dist ├── tests ├── CopyTest.php ├── InstanceTest.php ├── IssetTest.php ├── CreateFromFormatTest.php ├── TestFixture.php ├── CreateFromTimestampTest.php ├── CreateFromDateTest.php ├── StartEndOfTest.php ├── CreateFromTimeTest.php ├── NowAndOtherStaticHelpersTest.php ├── ConstructTest.php ├── IsTest.php ├── FluidSettersTest.php ├── CreateTest.php ├── StringsTest.php ├── SubTest.php ├── AddTest.php ├── TestingAidsTest.php ├── SettersTest.php ├── ComparisonTest.php ├── GettersTest.php ├── DayOfWeekModifiersTest.php └── DiffTest.php ├── LICENSE ├── history.md ├── readme.php ├── readme.md └── readme.src.md /.gitignore: -------------------------------------------------------------------------------- 1 | vendor 2 | composer.phar -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | 8 | before_script: 9 | - composer install 10 | 11 | script: phpunit --coverage-text -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nesbot/carbon", 3 | "type": "library", 4 | "description": "A simple API extension for DateTime.", 5 | "keywords": [ 6 | "date", 7 | "time", 8 | "DateTime" 9 | ], 10 | "homepage": "https://github.com/briannesbitt/Carbon", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Brian Nesbitt", 15 | "email": "brian@nesbot.com", 16 | "homepage": "http://nesbot.com" 17 | } 18 | ], 19 | "require": { 20 | "php": ">=5.3.0" 21 | }, 22 | "require-dev": { 23 | "phpunit/phpunit": "3.7.*" 24 | }, 25 | "autoload": { 26 | "psr-0": { 27 | "Carbon": "src" 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | 16 | src/Carbon 17 | 18 | 19 | 20 | 21 | 22 | tests 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/CopyTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CopyTest extends TestFixture 15 | { 16 | public function testCopy() 17 | { 18 | $dating = Carbon::now(); 19 | $dating2 = $dating->copy(); 20 | $this->assertNotSame($dating, $dating2); 21 | } 22 | 23 | public function testCopyEnsureTzIsCopied() 24 | { 25 | $dating = Carbon::createFromDate(2000, 1, 1, 'Europe/London'); 26 | $dating2 = $dating->copy(); 27 | $this->assertSame($dating->tzName, $dating2->tzName); 28 | $this->assertSame($dating->offset, $dating2->offset); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /tests/InstanceTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class InstanceTest extends TestFixture 15 | { 16 | public function testInstanceFromDateTime() 17 | { 18 | $dating = Carbon::instance(\DateTime::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11')); 19 | $this->assertCarbon($dating, 1975, 5, 21, 22, 32, 11); 20 | } 21 | 22 | public function testInstanceFromDateTimeKeepsTimezoneName() 23 | { 24 | $dating = Carbon::instance(\DateTime::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11')->setTimezone(new \DateTimeZone('America/Vancouver'))); 25 | $this->assertSame('America/Vancouver', $dating->tzName); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /tests/IssetTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class IssetTest extends TestFixture 15 | { 16 | public function testIssetReturnFalseForUnknownProperty() 17 | { 18 | $this->assertFalse(isset(Carbon::create(1234, 5, 6, 7, 8, 9)->sdfsdfss)); 19 | } 20 | public function testIssetReturnTrueForProperties() 21 | { 22 | $properties = array('year', 'month', 'day', 'hour', 'minute', 'second', 'dayOfWeek', 'dayOfYear', 'daysInMonth', 'timestamp', 'age', 'quarter', 'dst', 'offset', 'offsetHours', 'timezone', 'timezoneName', 'tz', 'tzName'); 23 | foreach ($properties as $property) { 24 | $this->assertTrue(isset(Carbon::create(1234, 5, 6, 7, 8, 9)->$property)); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) Brian Nesbitt 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. -------------------------------------------------------------------------------- /tests/CreateFromFormatTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CreateFromFormatTest extends TestFixture 15 | { 16 | public function testCreateFromFormatReturnsCarbon() 17 | { 18 | $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11'); 19 | $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11); 20 | $this->assertTrue($d instanceof Carbon); 21 | } 22 | 23 | public function testCreateFromFormatWithTimezoneString() 24 | { 25 | $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', 'Europe/London'); 26 | $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11); 27 | $this->assertSame('Europe/London', $d->tzName); 28 | } 29 | 30 | public function testCreateFromFormatWithTimezone() 31 | { 32 | $d = Carbon::createFromFormat('Y-m-d H:i:s', '1975-05-21 22:32:11', new \DateTimeZone('Europe/London')); 33 | $this->assertCarbon($d, 1975, 5, 21, 22, 32, 11); 34 | $this->assertSame('Europe/London', $d->tzName); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /tests/TestFixture.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | require __DIR__.'/../vendor/autoload.php'; 13 | 14 | use Carbon\Carbon; 15 | 16 | class TestFixture extends \PHPUnit_Framework_TestCase 17 | { 18 | private $saveTz; 19 | 20 | protected function setUp() 21 | { 22 | //save current timezone 23 | $this->saveTz = date_default_timezone_get(); 24 | 25 | date_default_timezone_set('America/Toronto'); 26 | } 27 | 28 | protected function tearDown() 29 | { 30 | date_default_timezone_set($this->saveTz); 31 | } 32 | 33 | protected function assertCarbon(Carbon $d, $year, $month, $day, $hour = null, $minute = null, $second = null) 34 | { 35 | $this->assertSame($year, $d->year, 'Carbon->year'); 36 | $this->assertSame($month, $d->month, 'Carbon->month'); 37 | $this->assertSame($day, $d->day, 'Carbon->day'); 38 | 39 | if ($hour !== null) { 40 | $this->assertSame($hour, $d->hour, 'Carbon->hour'); 41 | } 42 | 43 | if ($minute !== null) { 44 | $this->assertSame($minute, $d->minute, 'Carbon->minute'); 45 | } 46 | 47 | if ($second !== null) { 48 | $this->assertSame($second, $d->second, 'Carbon->second'); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/CreateFromTimestampTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CreateFromTimestampTest extends TestFixture 15 | { 16 | public function testCreateReturnsDatingInstance() 17 | { 18 | $d = Carbon::createFromTimestamp(Carbon::create(1975, 5, 21, 22, 32, 5)->timestamp); 19 | $this->assertCarbon($d, 1975, 5, 21, 22, 32, 5); 20 | } 21 | 22 | public function testCreateFromTimestampUsesDefaultTimezone() 23 | { 24 | $d = Carbon::createFromTimestamp(0); 25 | 26 | // We know Toronto is -5 since no DST in Jan 27 | $this->assertSame(1969, $d->year); 28 | $this->assertSame(-5 * 3600, $d->offset); 29 | } 30 | 31 | public function testCreateFromTimestampWithDateTimeZone() 32 | { 33 | $d = Carbon::createFromTimestamp(0, new \DateTimeZone('UTC')); 34 | $this->assertSame('UTC', $d->tzName); 35 | $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0); 36 | } 37 | public function testCreateFromTimestampWithString() 38 | { 39 | $d = Carbon::createFromTimestamp(0, 'UTC'); 40 | $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0); 41 | $this->assertTrue($d->offset === 0); 42 | $this->assertSame('UTC', $d->tzName); 43 | } 44 | 45 | public function testCreateFromTimestampGMTDoesNotUseDefaultTimezone() 46 | { 47 | $d = Carbon::createFromTimestampUTC(0); 48 | $this->assertCarbon($d, 1970, 1, 1, 0, 0, 0); 49 | $this->assertTrue($d->offset === 0); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests/CreateFromDateTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CreateFromDateTest extends TestFixture 15 | { 16 | public function testCreateFromDateWithDefaults() 17 | { 18 | $d = Carbon::createFromDate(); 19 | $this->assertSame($d->timestamp, Carbon::create(null, null, null, null, null, null)->timestamp); 20 | } 21 | 22 | public function testCreateFromDate() 23 | { 24 | $d = Carbon::createFromDate(1975, 5, 21); 25 | $this->assertCarbon($d, 1975, 5, 21); 26 | } 27 | 28 | public function testCreateFromDateWithYear() 29 | { 30 | $d = Carbon::createFromDate(1975); 31 | $this->assertSame(1975, $d->year); 32 | } 33 | 34 | public function testCreateFromDateWithMonth() 35 | { 36 | $d = Carbon::createFromDate(null, 5); 37 | $this->assertSame(5, $d->month); 38 | } 39 | 40 | public function testCreateFromDateWithDay() 41 | { 42 | $d = Carbon::createFromDate(null, null, 21); 43 | $this->assertSame(21, $d->day); 44 | } 45 | 46 | public function testCreateFromDateWithTimezone() 47 | { 48 | $d = Carbon::createFromDate(1975, 5, 21, 'Europe/London'); 49 | $this->assertCarbon($d, 1975, 5, 21); 50 | $this->assertSame('Europe/London', $d->tzName); 51 | } 52 | 53 | public function testCreateFromDateWithDateTimeZone() 54 | { 55 | $d = Carbon::createFromDate(1975, 5, 21, new \DateTimeZone('Europe/London')); 56 | $this->assertCarbon($d, 1975, 5, 21); 57 | $this->assertSame('Europe/London', $d->tzName); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /history.md: -------------------------------------------------------------------------------- 1 | 1.5.0 / Unreleased 2 | ================== 3 | * Diff for humans now shows 2 weeks ago instead of 14 days ago 4 | * Added a local getter to test if the instance is in the local timezone 5 | * Added a utc getter to check if the instance is in UTC timezone 6 | * Fixed dst comment / phpdoc 7 | * Optimize timezone getters 8 | 9 | 1.4.0 / 2013-09-08 10 | ================== 11 | * Corrected various PHPdocs 12 | * formatLocalized() is now more OS independent 13 | * Improved diff methods 14 | * Test now can be mocked using a relative term 15 | 16 | 1.3.0 / 2013-08-21 17 | ================== 18 | 19 | * Added modifier methods firstOfMonth(), lastOfMonth(), nthOfMonth(), next(), previous(), and so on 20 | * Added modifiers startOfWeek() and endOfWeek() 21 | * Added testing helpers to allow mocking of new Carbon(), new Carbon('now') and Carbon::now() 22 | * Added formatLocalized() to format a string using strftime() with the current locale 23 | * Improved diffInSeconds() 24 | * Improved [add|sub][Years|Months|Days|Hours|Minutes|Seconds|Weeks] 25 | * Docblocks everywhere ;( 26 | * Magic class properties 27 | * Added PHP 5.5 to travis test coverage 28 | * General Code cleanup 29 | 30 | 1.2.0 / 2012-10-14 31 | ================== 32 | 33 | * Added history.md 34 | * Implemented __isset() (thanks @flevour) 35 | * Simplified tomorrow()/yesterday() to rely on today()... more DRY 36 | * Simplified __set() and fixed exception text 37 | * Updated readme 38 | 39 | 1.1.0 / 2012-09-16 40 | ================== 41 | 42 | * Updated composer.json 43 | * Added better error messaging for failed readme generation 44 | * Fixed readme typos 45 | * Added static helpers `today()`, `tomorrow()`, `yesterday()` 46 | * Simplified `now()` code 47 | 48 | 1.0.1 / 2012-09-10 49 | ================== 50 | 51 | * Added travis-ci.org 52 | 53 | 1.0.0 / 2012-09-10 54 | ================== 55 | 56 | * Initial release 57 | -------------------------------------------------------------------------------- /tests/StartEndOfTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class StartEndOfTest extends TestFixture 15 | { 16 | public function testStartOfDay() 17 | { 18 | $dt = Carbon::now(); 19 | $this->assertTrue($dt->startOfDay() instanceof Carbon); 20 | $this->assertCarbon($dt, $dt->year, $dt->month, $dt->day, 0, 0, 0); 21 | } 22 | public function testEndOfDay() 23 | { 24 | $dt = Carbon::now(); 25 | $this->assertTrue($dt->endOfDay() instanceof Carbon); 26 | $this->assertCarbon($dt, $dt->year, $dt->month, $dt->day, 23, 59, 59); 27 | } 28 | 29 | public function testStartOfMonthIsFluid() 30 | { 31 | $dt = Carbon::now(); 32 | $this->assertTrue($dt->startOfMonth() instanceof Carbon); 33 | } 34 | public function testStartOfMonthFromNow() 35 | { 36 | $dt = Carbon::now()->startOfMonth(); 37 | $this->assertCarbon($dt, $dt->year, $dt->month, 1, 0, 0, 0); 38 | } 39 | public function testStartOfMonthFromLastDay() 40 | { 41 | $dt = Carbon::create(2000, 1, 31, 2, 3, 4)->startOfMonth(); 42 | $this->assertCarbon($dt, 2000, 1, 1, 0, 0, 0); 43 | } 44 | 45 | public function testEndOfMonthIsFluid() 46 | { 47 | $dt = Carbon::now(); 48 | $this->assertTrue($dt->endOfMonth() instanceof Carbon); 49 | } 50 | public function testEndOfMonth() 51 | { 52 | $dt = Carbon::create(2000, 1, 1, 2, 3, 4)->endOfMonth(); 53 | $this->assertCarbon($dt, 2000, 1, 31, 23, 59, 59); 54 | } 55 | public function testEndOfMonthFromLastDay() 56 | { 57 | $dt = Carbon::create(2000, 1, 31, 2, 3, 4)->endOfMonth(); 58 | $this->assertCarbon($dt, 2000, 1, 31, 23, 59, 59); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/CreateFromTimeTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CreateFromTimeTest extends TestFixture 15 | { 16 | public function testCreateFromDateWithDefaults() 17 | { 18 | $d = Carbon::createFromTime(); 19 | $this->assertSame($d->timestamp, Carbon::create(null, null, null, null, null, null)->timestamp); 20 | } 21 | 22 | public function testCreateFromDate() 23 | { 24 | $d = Carbon::createFromTime(23, 5, 21); 25 | $this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 23, 5, 21); 26 | } 27 | 28 | public function testCreateFromTimeWithHour() 29 | { 30 | $d = Carbon::createFromTime(22); 31 | $this->assertSame(22, $d->hour); 32 | $this->assertSame(0, $d->minute); 33 | $this->assertSame(0, $d->second); 34 | } 35 | 36 | public function testCreateFromTimeWithMinute() 37 | { 38 | $d = Carbon::createFromTime(null, 5); 39 | $this->assertSame(5, $d->minute); 40 | } 41 | 42 | public function testCreateFromTimeWithSecond() 43 | { 44 | $d = Carbon::createFromTime(null, null, 21); 45 | $this->assertSame(21, $d->second); 46 | } 47 | 48 | public function testCreateFromTimeWithDateTimeZone() 49 | { 50 | $d = Carbon::createFromTime(12, 0, 0, new \DateTimeZone('Europe/London')); 51 | $this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 12, 0, 0); 52 | $this->assertSame('Europe/London', $d->tzName); 53 | } 54 | public function testCreateFromTimeWithTimeZoneString() 55 | { 56 | $d = Carbon::createFromTime(12, 0, 0, 'Europe/London'); 57 | $this->assertCarbon($d, Carbon::now()->year, Carbon::now()->month, Carbon::now()->day, 12, 0, 0); 58 | $this->assertSame('Europe/London', $d->tzName); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /tests/NowAndOtherStaticHelpersTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class NowAndOtherStaticHelpersTest extends TestFixture 15 | { 16 | public function testNow() 17 | { 18 | $dt = Carbon::now(); 19 | $this->assertSame(time(), $dt->timestamp); 20 | } 21 | public function testNowWithTimezone() 22 | { 23 | $dt = Carbon::now('Europe/London'); 24 | $this->assertSame(time(), $dt->timestamp); 25 | $this->assertSame('Europe/London', $dt->tzName); 26 | } 27 | 28 | public function testToday() 29 | { 30 | $dt = Carbon::today(); 31 | $this->assertSame(date('Y-m-d 00:00:00'), $dt->toDateTimeString()); 32 | } 33 | public function testTodayWithTimezone() 34 | { 35 | $dt = Carbon::today('Europe/London'); 36 | $dt2 = new \DateTime('now', new \DateTimeZone('Europe/London')); 37 | $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString()); 38 | } 39 | 40 | public function testTomorrow() 41 | { 42 | $dt = Carbon::tomorrow(); 43 | $dt2 = new \DateTime('tomorrow'); 44 | $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString()); 45 | } 46 | public function testTomorrowWithTimezone() 47 | { 48 | $dt = Carbon::tomorrow('Europe/London'); 49 | $dt2 = new \DateTime('tomorrow', new \DateTimeZone('Europe/London')); 50 | $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString()); 51 | } 52 | 53 | public function testYesterday() 54 | { 55 | $dt = Carbon::yesterday(); 56 | $dt2 = new \DateTime('yesterday'); 57 | $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString()); 58 | } 59 | public function testYesterdayWithTimezone() 60 | { 61 | $dt = Carbon::yesterday('Europe/London'); 62 | $dt2 = new \DateTime('yesterday', new \DateTimeZone('Europe/London')); 63 | $this->assertSame($dt2->format('Y-m-d 00:00:00'), $dt->toDateTimeString()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /readme.php: -------------------------------------------------------------------------------- 1 | 6 | * 7 | * For the full copyright and license information, please view the LICENSE 8 | * file that was distributed with this source code. 9 | */ 10 | 11 | require 'src/Carbon/Carbon.php'; 12 | 13 | use Carbon\Carbon; 14 | 15 | date_default_timezone_set('America/Toronto'); 16 | 17 | $readme = file_get_contents('readme.src.md'); 18 | 19 | $pre_src = 'use Carbon\Carbon; '; 20 | 21 | // {{intro::exec(echo Carbon::now()->subMinutes(2)->diffForHumans();)}} 22 | preg_match_all('@{{(\w*)::(\w+)\((.+)\)}}@sU', $readme, $matches, PREG_SET_ORDER); 23 | 24 | foreach ($matches as $match) { 25 | 26 | list($orig, $name, $cmd, $src) = $match; 27 | 28 | $src = trim($src, "\n\r"); 29 | 30 | ob_start(); 31 | $result = eval($pre_src . $src); 32 | 33 | $ob = ob_get_clean(); 34 | 35 | if ($result === false) { 36 | echo "Failed lint check.". PHP_EOL . PHP_EOL; 37 | 38 | $error = error_get_last(); 39 | if ($error != null) { 40 | echo $error['message'] . ' on line ' . $error['line'] . PHP_EOL . PHP_EOL; 41 | } 42 | 43 | echo "---- eval'd source ---- " . PHP_EOL . PHP_EOL; 44 | 45 | $i = 1; 46 | foreach (preg_split("/$[\n\r]^/m", $src) as $ln) { 47 | printf('%3s : %s%s', $i++, $ln, PHP_EOL); 48 | } 49 | 50 | exit(1); 51 | } 52 | 53 | // remove the extra newline from a var_dump 54 | if (strpos($src, 'var_dump(') === 0) { 55 | $ob = trim($ob); 56 | } 57 | 58 | // Add any necessary padding to lineup comments 59 | if (preg_match('@/\*pad\(([0-9]+)\)\*/@', $src, $matches)) { 60 | $src = preg_replace('@/\*pad\(([0-9]+)\)\*/@', '', $src); 61 | $src = str_pad($src, intval($matches[1])); 62 | } 63 | 64 | // Inject the source code 65 | $readme = str_replace($orig, $src, $readme); 66 | 67 | // Inject the eval'd result 68 | if ($cmd == 'exec') { 69 | $readme = str_replace('{{'.$name.'_eval}}', $ob, $readme); 70 | } 71 | } 72 | 73 | // allow for escaping a command 74 | $readme = str_replace('\{\{', '{{', $readme); 75 | 76 | file_put_contents('readme.md', $readme); 77 | -------------------------------------------------------------------------------- /tests/ConstructTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class ConstructTest extends TestFixture 15 | { 16 | public function testCreatesAnInstanceDefaultToNow() 17 | { 18 | $c = new Carbon(); 19 | $now = Carbon::now(); 20 | $this->assertEquals('Carbon\Carbon', get_class($c)); 21 | $this->assertEquals($now->tzName, $c->tzName); 22 | $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second); 23 | } 24 | public function testParseCreatesAnInstanceDefaultToNow() 25 | { 26 | $c = Carbon::parse(); 27 | $now = Carbon::now(); 28 | $this->assertEquals('Carbon\Carbon', get_class($c)); 29 | $this->assertEquals($now->tzName, $c->tzName); 30 | $this->assertCarbon($c, $now->year, $now->month, $now->day, $now->hour, $now->minute, $now->second); 31 | } 32 | 33 | public function testWithFancyString() 34 | { 35 | $c = new Carbon('first day of January 2008'); 36 | $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0); 37 | } 38 | public function testParseWithFancyString() 39 | { 40 | $c = Carbon::parse('first day of January 2008'); 41 | $this->assertCarbon($c, 2008, 1, 1, 0, 0, 0); 42 | } 43 | 44 | public function testDefaultTimezone() 45 | { 46 | $c = new Carbon('now'); 47 | $this->assertSame('America/Toronto', $c->tzName); 48 | } 49 | public function testParseWithDefaultTimezone() 50 | { 51 | $c = Carbon::parse('now'); 52 | $this->assertSame('America/Toronto', $c->tzName); 53 | } 54 | 55 | public function testSettingTimezone() 56 | { 57 | $c = new Carbon('now', new \DateTimeZone('Europe/London')); 58 | $this->assertSame('Europe/London', $c->tzName); 59 | $this->assertSame(0, $c->offsetHours); 60 | } 61 | public function testParseSettingTimezone() 62 | { 63 | $c = Carbon::parse('now', new \DateTimeZone('Europe/London')); 64 | $this->assertSame('Europe/London', $c->tzName); 65 | $this->assertSame(0, $c->offsetHours); 66 | } 67 | 68 | public function testSettingTimezoneWithString() 69 | { 70 | $c = new Carbon('now', 'Asia/Tokyo'); 71 | $this->assertSame('Asia/Tokyo', $c->tzName); 72 | $this->assertSame(9, $c->offsetHours); 73 | } 74 | public function testParseSettingTimezoneWithString() 75 | { 76 | $c = Carbon::parse('now', 'Asia/Tokyo'); 77 | $this->assertSame('Asia/Tokyo', $c->tzName); 78 | $this->assertSame(9, $c->offsetHours); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /tests/IsTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class IsTest extends TestFixture 15 | { 16 | public function testIsWeekdayTrue() 17 | { 18 | $this->assertTrue(Carbon::createFromDate(2012, 1, 2)->isWeekday()); 19 | } 20 | public function testIsWeekdayFalse() 21 | { 22 | $this->assertFalse(Carbon::createFromDate(2012, 1, 1)->isWeekday()); 23 | } 24 | public function testIsWeekendTrue() 25 | { 26 | $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isWeekend()); 27 | } 28 | public function testIsWeekendFalse() 29 | { 30 | $this->assertFalse(Carbon::createFromDate(2012, 1, 2)->isWeekend()); 31 | } 32 | 33 | public function testIsYesterdayTrue() 34 | { 35 | $this->assertTrue(Carbon::now()->subDay()->isYesterday()); 36 | } 37 | public function testIsYesterdayFalseWithToday() 38 | { 39 | $this->assertFalse(Carbon::now()->endOfDay()->isYesterday()); 40 | } 41 | public function testIsYesterdayFalseWith2Days() 42 | { 43 | $this->assertFalse(Carbon::now()->subDays(2)->startOfDay()->isYesterday()); 44 | } 45 | 46 | public function testIsTodayTrue() 47 | { 48 | $this->assertTrue(Carbon::now()->isToday()); 49 | } 50 | public function testIsTodayFalseWithYesterday() 51 | { 52 | $this->assertFalse(Carbon::now()->subDay()->endOfDay()->isToday()); 53 | } 54 | public function testIsTodayFalseWithTomorrow() 55 | { 56 | $this->assertFalse(Carbon::now()->addDay()->startOfDay()->isToday()); 57 | } 58 | public function testIsTodayWithTimezone() 59 | { 60 | $this->assertTrue(Carbon::now('Asia/Tokyo')->isToday()); 61 | } 62 | 63 | public function testIsTomorrowTrue() 64 | { 65 | $this->assertTrue(Carbon::now()->addDay()->isTomorrow()); 66 | } 67 | public function testIsTomorrowFalseWithToday() 68 | { 69 | $this->assertFalse(Carbon::now()->endOfDay()->isTomorrow()); 70 | } 71 | public function testIsTomorrowFalseWith2Days() 72 | { 73 | $this->assertFalse(Carbon::now()->addDays(2)->startOfDay()->isTomorrow()); 74 | } 75 | 76 | public function testIsFutureTrue() 77 | { 78 | $this->assertTrue(Carbon::now()->addSecond()->isFuture()); 79 | } 80 | public function testIsFutureFalse() 81 | { 82 | $this->assertFalse(Carbon::now()->isFuture()); 83 | } 84 | public function testIsFutureFalseInThePast() 85 | { 86 | $this->assertFalse(Carbon::now()->subSecond()->isFuture()); 87 | } 88 | 89 | public function testIsPastTrue() 90 | { 91 | $this->assertTrue(Carbon::now()->subSecond()->isPast()); 92 | } 93 | public function testIsPast() 94 | { 95 | $this->assertFalse(Carbon::now()->addSecond()->isPast()); 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /tests/FluidSettersTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class FluidSettersTest extends TestFixture 15 | { 16 | public function testFluidYearSetter() 17 | { 18 | $d = Carbon::now(); 19 | $this->assertTrue($d->year(1995) instanceof Carbon); 20 | $this->assertSame(1995, $d->year); 21 | } 22 | 23 | public function testFluidMonthSetter() 24 | { 25 | $d = Carbon::now(); 26 | $this->assertTrue($d->month(3) instanceof Carbon); 27 | $this->assertSame(3, $d->month); 28 | } 29 | public function testFluidMonthSetterWithWrap() 30 | { 31 | $d = Carbon::createFromDate(2012, 8, 21); 32 | $this->assertTrue($d->month(13) instanceof Carbon); 33 | $this->assertSame(1, $d->month); 34 | } 35 | 36 | public function testFluidDaySetter() 37 | { 38 | $d = Carbon::now(); 39 | $this->assertTrue($d->day(2) instanceof Carbon); 40 | $this->assertSame(2, $d->day); 41 | } 42 | public function testFluidDaySetterWithWrap() 43 | { 44 | $d = Carbon::createFromDate(2000, 1, 1); 45 | $this->assertTrue($d->day(32) instanceof Carbon); 46 | $this->assertSame(1, $d->day); 47 | } 48 | 49 | public function testFluidSetDate() 50 | { 51 | $d = Carbon::createFromDate(2000, 1, 1); 52 | $this->assertTrue($d->setDate(1995, 13, 32) instanceof Carbon); 53 | $this->assertCarbon($d, 1996, 2, 1); 54 | } 55 | 56 | public function testFluidHourSetter() 57 | { 58 | $d = Carbon::now(); 59 | $this->assertTrue($d->hour(2) instanceof Carbon); 60 | $this->assertSame(2, $d->hour); 61 | } 62 | public function testFluidHourSetterWithWrap() 63 | { 64 | $d = Carbon::now(); 65 | $this->assertTrue($d->hour(25) instanceof Carbon); 66 | $this->assertSame(1, $d->hour); 67 | } 68 | 69 | public function testFluidMinuteSetter() 70 | { 71 | $d = Carbon::now(); 72 | $this->assertTrue($d->minute(2) instanceof Carbon); 73 | $this->assertSame(2, $d->minute); 74 | } 75 | public function testFluidMinuteSetterWithWrap() 76 | { 77 | $d = Carbon::now(); 78 | $this->assertTrue($d->minute(61) instanceof Carbon); 79 | $this->assertSame(1, $d->minute); 80 | } 81 | 82 | public function testFluidSecondSetter() 83 | { 84 | $d = Carbon::now(); 85 | $this->assertTrue($d->second(2) instanceof Carbon); 86 | $this->assertSame(2, $d->second); 87 | } 88 | public function testFluidSecondSetterWithWrap() 89 | { 90 | $d = Carbon::now(); 91 | $this->assertTrue($d->second(62) instanceof Carbon); 92 | $this->assertSame(2, $d->second); 93 | } 94 | 95 | public function testFluidSetTime() 96 | { 97 | $d = Carbon::createFromDate(2000, 1, 1); 98 | $this->assertTrue($d->setTime(25, 61, 61) instanceof Carbon); 99 | $this->assertCarbon($d, 2000, 1, 2, 2, 2, 1); 100 | } 101 | 102 | public function testFluidTimestampSetter() 103 | { 104 | $d = Carbon::now(); 105 | $this->assertTrue($d->timestamp(10) instanceof Carbon); 106 | $this->assertSame(10, $d->timestamp); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /tests/CreateTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class CreateTest extends TestFixture 15 | { 16 | public function testCreateReturnsDatingInstance() 17 | { 18 | $d = Carbon::create(); 19 | $this->assertTrue($d instanceof Carbon); 20 | } 21 | 22 | public function testCreateWithDefaults() 23 | { 24 | $d = Carbon::create(); 25 | $this->assertSame($d->timestamp, Carbon::now()->timestamp); 26 | } 27 | 28 | public function testCreateWithYear() 29 | { 30 | $d = Carbon::create(2012); 31 | $this->assertSame(2012, $d->year); 32 | } 33 | public function testCreateWithInvalidYear() 34 | { 35 | $this->setExpectedException('InvalidArgumentException'); 36 | $d = Carbon::create(-3); 37 | } 38 | 39 | public function testCreateWithMonth() 40 | { 41 | $d = Carbon::create(null, 3); 42 | $this->assertSame(3, $d->month); 43 | } 44 | public function testCreateWithInvalidMonth() 45 | { 46 | $this->setExpectedException('InvalidArgumentException'); 47 | $d = Carbon::create(null, -5); 48 | } 49 | public function testCreateMonthWraps() 50 | { 51 | $d = Carbon::create(2011, 0, 1, 0, 0, 0); 52 | $this->assertCarbon($d, 2010, 12, 1, 0, 0, 0); 53 | } 54 | 55 | public function testCreateWithDay() 56 | { 57 | $d = Carbon::create(null, null, 21); 58 | $this->assertSame(21, $d->day); 59 | } 60 | public function testCreateWithInvalidDay() 61 | { 62 | $this->setExpectedException('InvalidArgumentException'); 63 | $d = Carbon::create(null, null, -4); 64 | } 65 | public function testCreateDayWraps() 66 | { 67 | $d = Carbon::create(2011, 1, 40, 0, 0, 0); 68 | $this->assertCarbon($d, 2011, 2, 9, 0, 0, 0); 69 | } 70 | 71 | public function testCreateWithHourAndDefaultMinSecToZero() 72 | { 73 | $d = Carbon::create(null, null, null, 14); 74 | $this->assertSame(14, $d->hour); 75 | $this->assertSame(0, $d->minute); 76 | $this->assertSame(0, $d->second); 77 | } 78 | public function testCreateWithInvalidHour() 79 | { 80 | $this->setExpectedException('InvalidArgumentException'); 81 | $d = Carbon::create(null, null, null, -1); 82 | } 83 | public function testCreateHourWraps() 84 | { 85 | $d = Carbon::create(2011, 1, 1, 24, 0, 0); 86 | $this->assertCarbon($d, 2011, 1, 2, 0, 0, 0); 87 | } 88 | 89 | public function testCreateWithMinute() 90 | { 91 | $d = Carbon::create(null, null, null, null, 58); 92 | $this->assertSame(58, $d->minute); 93 | } 94 | public function testCreateWithInvalidMinute() 95 | { 96 | $this->setExpectedException('InvalidArgumentException'); 97 | $d = Carbon::create(2011, 1, 1, 0, -2, 0); 98 | } 99 | public function testCreateMinuteWraps() 100 | { 101 | $d = Carbon::create(2011, 1, 1, 0, 62, 0); 102 | $this->assertCarbon($d, 2011, 1, 1, 1, 2, 0); 103 | } 104 | 105 | public function testCreateWithSecond() 106 | { 107 | $d = Carbon::create(null, null, null, null, null, 59); 108 | $this->assertSame(59, $d->second); 109 | } 110 | public function testCreateWithInvalidSecond() 111 | { 112 | $this->setExpectedException('InvalidArgumentException'); 113 | $d = Carbon::create(null, null, null, null, null, -2); 114 | } 115 | public function testCreateSecondsWrap() 116 | { 117 | $d = Carbon::create(2012, 1, 1, 0, 0, 61); 118 | $this->assertCarbon($d, 2012, 1, 1, 0, 1, 1); 119 | } 120 | 121 | public function testCreateWithDateTimeZone() 122 | { 123 | $d = Carbon::create(2012, 1, 1, 0, 0, 0, new \DateTimeZone('Europe/London')); 124 | $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0); 125 | $this->assertSame('Europe/London', $d->tzName); 126 | } 127 | public function testCreateWithTimeZoneString() 128 | { 129 | $d = Carbon::create(2012, 1, 1, 0, 0, 0, 'Europe/London'); 130 | $this->assertCarbon($d, 2012, 1, 1, 0, 0, 0); 131 | $this->assertSame('Europe/London', $d->tzName); 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /tests/StringsTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class StringsTest extends TestFixture 15 | { 16 | public function testToString() 17 | { 18 | $d = Carbon::now(); 19 | $this->assertSame(Carbon::now()->toDateTimeString(), ''.$d); 20 | } 21 | 22 | public function testToDateString() 23 | { 24 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 25 | $this->assertSame('1975-12-25', $d->toDateString()); 26 | } 27 | public function testToFormattedDateString() 28 | { 29 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 30 | $this->assertSame('Dec 25, 1975', $d->toFormattedDateString()); 31 | } 32 | public function testToLocalizedFormattedDateString() 33 | { 34 | /**************** 35 | 36 | Working out a Travis issue on how to set a different locale 37 | other than EN to test this. 38 | 39 | 40 | $cache = setlocale(LC_TIME, 0); 41 | setlocale(LC_TIME, 'German'); 42 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 43 | $this->assertSame('Donnerstag 25 Dezember 1975', $d->formatLocalized('%A %d %B %Y')); 44 | setlocale(LC_TIME, $cache); 45 | 46 | *****************/ 47 | } 48 | public function testToTimeString() 49 | { 50 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 51 | $this->assertSame('14:15:16', $d->toTimeString()); 52 | } 53 | public function testToDateTimeString() 54 | { 55 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 56 | $this->assertSame('1975-12-25 14:15:16', $d->toDateTimeString()); 57 | } 58 | public function testToDateTimeStringWithPaddedZeroes() 59 | { 60 | $d = Carbon::create(2000, 5, 2, 4, 3, 4); 61 | $this->assertSame('2000-05-02 04:03:04', $d->toDateTimeString()); 62 | } 63 | public function testToDayDateTimeString() 64 | { 65 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 66 | $this->assertSame('Thu, Dec 25, 1975 2:15 PM', $d->toDayDateTimeString()); 67 | } 68 | 69 | public function testToATOMString() 70 | { 71 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 72 | $this->assertSame('1975-12-25T14:15:16-05:00', $d->toATOMString()); 73 | } 74 | public function testToCOOKIEString() 75 | { 76 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 77 | $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toCOOKIEString()); 78 | } 79 | public function testToISO8601String() 80 | { 81 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 82 | $this->assertSame('1975-12-25T14:15:16-0500', $d->toISO8601String()); 83 | } 84 | public function testToRC822String() 85 | { 86 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 87 | $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRFC822String()); 88 | } 89 | public function testToRFC850String() 90 | { 91 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 92 | $this->assertSame('Thursday, 25-Dec-75 14:15:16 EST', $d->toRFC850String()); 93 | } 94 | public function testToRFC1036String() 95 | { 96 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 97 | $this->assertSame('Thu, 25 Dec 75 14:15:16 -0500', $d->toRFC1036String()); 98 | } 99 | public function testToRFC1123String() 100 | { 101 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 102 | $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRFC1123String()); 103 | } 104 | public function testToRFC2822String() 105 | { 106 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 107 | $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRFC2822String()); 108 | } 109 | public function testToRFC3339String() 110 | { 111 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 112 | $this->assertSame('1975-12-25T14:15:16-05:00', $d->toRFC3339String()); 113 | } 114 | public function testToRSSString() 115 | { 116 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 117 | $this->assertSame('Thu, 25 Dec 1975 14:15:16 -0500', $d->toRSSString()); 118 | } 119 | public function testToW3CString() 120 | { 121 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 122 | $this->assertSame('1975-12-25T14:15:16-05:00', $d->toW3CString()); 123 | } 124 | public function testChangeDefaultFormat() 125 | { 126 | Carbon::setDefaultFormat('jS \o\f F, Y g:i:s a'); 127 | $d = Carbon::create(1975, 12, 25, 14, 15, 16); 128 | $this->assertSame('25th of December, 1975 2:15:16 pm', ''.$d); 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /tests/SubTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class SubTest extends TestFixture 15 | { 16 | public function testSubYearsPositive() 17 | { 18 | $this->assertSame(1974, Carbon::createFromDate(1975)->subYears(1)->year); 19 | } 20 | public function testSubYearsZero() 21 | { 22 | $this->assertSame(1975, Carbon::createFromDate(1975)->subYears(0)->year); 23 | } 24 | public function testSubYearsNegative() 25 | { 26 | $this->assertSame(1976, Carbon::createFromDate(1975)->subYears(-1)->year); 27 | } 28 | 29 | public function testSubYear() 30 | { 31 | $this->assertSame(1974, Carbon::createFromDate(1975)->subYear()->year); 32 | } 33 | 34 | public function testSubMonthsPositive() 35 | { 36 | $this->assertSame(12, Carbon::createFromDate(1975, 1, 1)->subMonths(1)->month); 37 | } 38 | public function testSubMonthsZero() 39 | { 40 | $this->assertSame(1, Carbon::createFromDate(1975, 1, 1)->subMonths(0)->month); 41 | } 42 | public function testSubMonthsNegative() 43 | { 44 | $this->assertSame(2, Carbon::createFromDate(1975, 1, 1)->subMonths(-1)->month); 45 | } 46 | 47 | public function testSubMonth() 48 | { 49 | $this->assertSame(12, Carbon::createFromDate(1975, 1, 1)->subMonth()->month); 50 | } 51 | 52 | public function testSubDaysPositive() 53 | { 54 | $this->assertSame(30, Carbon::createFromDate(1975, 5, 1)->subDays(1)->day); 55 | } 56 | public function testSubDaysZero() 57 | { 58 | $this->assertSame(1, Carbon::createFromDate(1975, 5, 1)->subDays(0)->day); 59 | } 60 | public function testSubDaysNegative() 61 | { 62 | $this->assertSame(2, Carbon::createFromDate(1975, 5, 1)->subDays(-1)->day); 63 | } 64 | 65 | public function testSubDay() 66 | { 67 | $this->assertSame(30, Carbon::createFromDate(1975, 5, 1)->subDay()->day); 68 | } 69 | 70 | public function testSubWeekdaysPositive() 71 | { 72 | $this->assertSame(22, Carbon::createFromDate(2012, 1, 4)->subWeekdays(9)->day); 73 | } 74 | public function testSubWeekdaysZero() 75 | { 76 | $this->assertSame(4, Carbon::createFromDate(2012, 1, 4)->subWeekdays(0)->day); 77 | } 78 | public function testSubWeekdaysNegative() 79 | { 80 | $this->assertSame(13, Carbon::createFromDate(2012, 1, 31)->subWeekdays(-9)->day); 81 | } 82 | 83 | public function testSubWeekday() 84 | { 85 | $this->assertSame(6, Carbon::createFromDate(2012, 1, 9)->subWeekday()->day); 86 | } 87 | 88 | public function testSubWeeksPositive() 89 | { 90 | $this->assertSame(14, Carbon::createFromDate(1975, 5, 21)->subWeeks(1)->day); 91 | } 92 | public function testSubWeeksZero() 93 | { 94 | $this->assertSame(21, Carbon::createFromDate(1975, 5, 21)->subWeeks(0)->day); 95 | } 96 | public function testSubWeeksNegative() 97 | { 98 | $this->assertSame(28, Carbon::createFromDate(1975, 5, 21)->subWeeks(-1)->day); 99 | } 100 | 101 | public function testSubWeek() 102 | { 103 | $this->assertSame(14, Carbon::createFromDate(1975, 5, 21)->subWeek()->day); 104 | } 105 | 106 | public function testSubHoursPositive() 107 | { 108 | $this->assertSame(23, Carbon::createFromTime(0)->subHours(1)->hour); 109 | } 110 | public function testSubHoursZero() 111 | { 112 | $this->assertSame(0, Carbon::createFromTime(0)->subHours(0)->hour); 113 | } 114 | public function testSubHoursNegative() 115 | { 116 | $this->assertSame(1, Carbon::createFromTime(0)->subHours(-1)->hour); 117 | } 118 | 119 | public function testSubHour() 120 | { 121 | $this->assertSame(23, Carbon::createFromTime(0)->subHour()->hour); 122 | } 123 | 124 | public function testSubMinutesPositive() 125 | { 126 | $this->assertSame(59, Carbon::createFromTime(0, 0)->subMinutes(1)->minute); 127 | } 128 | public function testSubMinutesZero() 129 | { 130 | $this->assertSame(0, Carbon::createFromTime(0, 0)->subMinutes(0)->minute); 131 | } 132 | public function testSubMinutesNegative() 133 | { 134 | $this->assertSame(1, Carbon::createFromTime(0, 0)->subMinutes(-1)->minute); 135 | } 136 | 137 | public function testSubMinute() 138 | { 139 | $this->assertSame(59, Carbon::createFromTime(0, 0)->subMinute()->minute); 140 | } 141 | 142 | public function testSubSecondsPositive() 143 | { 144 | $this->assertSame(59, Carbon::createFromTime(0, 0, 0)->subSeconds(1)->second); 145 | } 146 | public function testSubSecondsZero() 147 | { 148 | $this->assertSame(0, Carbon::createFromTime(0, 0, 0)->subSeconds(0)->second); 149 | } 150 | public function testSubSecondsNegative() 151 | { 152 | $this->assertSame(1, Carbon::createFromTime(0, 0, 0)->subSeconds(-1)->second); 153 | } 154 | 155 | public function testSubSecond() 156 | { 157 | $this->assertSame(59, Carbon::createFromTime(0, 0, 0)->subSecond()->second); 158 | } 159 | } 160 | -------------------------------------------------------------------------------- /tests/AddTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class AddTest extends TestFixture 15 | { 16 | public function testAddYearsPositive() 17 | { 18 | $this->assertSame(1976, Carbon::createFromDate(1975)->addYears(1)->year); 19 | } 20 | public function testAddYearsZero() 21 | { 22 | $this->assertSame(1975, Carbon::createFromDate(1975)->addYears(0)->year); 23 | } 24 | public function testAddYearsNegative() 25 | { 26 | $this->assertSame(1974, Carbon::createFromDate(1975)->addYears(-1)->year); 27 | } 28 | 29 | public function testAddYear() 30 | { 31 | $this->assertSame(1976, Carbon::createFromDate(1975)->addYear()->year); 32 | } 33 | 34 | public function testAddMonthsPositive() 35 | { 36 | $this->assertSame(1, Carbon::createFromDate(1975, 12)->addMonths(1)->month); 37 | } 38 | public function testAddMonthsZero() 39 | { 40 | $this->assertSame(12, Carbon::createFromDate(1975, 12)->addMonths(0)->month); 41 | } 42 | public function testAddMonthsNegative() 43 | { 44 | $this->assertSame(11, Carbon::createFromDate(1975, 12, 1)->addMonths(-1)->month); 45 | } 46 | 47 | public function testAddMonth() 48 | { 49 | $this->assertSame(1, Carbon::createFromDate(1975, 12)->addMonth()->month); 50 | } 51 | public function testAddMonthWithOverflow() 52 | { 53 | $this->assertSame(3, Carbon::createFromDate(2012, 1, 31)->addMonth()->month); 54 | } 55 | 56 | public function testAddDaysPositive() 57 | { 58 | $this->assertSame(1, Carbon::createFromDate(1975, 5, 31)->addDays(1)->day); 59 | } 60 | public function testAddDaysZero() 61 | { 62 | $this->assertSame(31, Carbon::createFromDate(1975, 5, 31)->addDays(0)->day); 63 | } 64 | public function testAddDaysNegative() 65 | { 66 | $this->assertSame(30, Carbon::createFromDate(1975, 5, 31)->addDays(-1)->day); 67 | } 68 | 69 | public function testAddDay() 70 | { 71 | $this->assertSame(1, Carbon::createFromDate(1975, 5, 31)->addDay()->day); 72 | } 73 | 74 | public function testAddWeekdaysPositive() 75 | { 76 | $this->assertSame(17, Carbon::createFromDate(2012, 1, 4)->addWeekdays(9)->day); 77 | } 78 | public function testAddWeekdaysZero() 79 | { 80 | $this->assertSame(4, Carbon::createFromDate(2012, 1, 4)->addWeekdays(0)->day); 81 | } 82 | public function testAddWeekdaysNegative() 83 | { 84 | $this->assertSame(18, Carbon::createFromDate(2012, 1, 31)->addWeekdays(-9)->day); 85 | } 86 | 87 | public function testAddWeekday() 88 | { 89 | $this->assertSame(9, Carbon::createFromDate(2012, 1, 6)->addWeekday()->day); 90 | } 91 | 92 | public function testAddWeeksPositive() 93 | { 94 | $this->assertSame(28, Carbon::createFromDate(1975, 5, 21)->addWeeks(1)->day); 95 | } 96 | public function testAddWeeksZero() 97 | { 98 | $this->assertSame(21, Carbon::createFromDate(1975, 5, 21)->addWeeks(0)->day); 99 | } 100 | public function testAddWeeksNegative() 101 | { 102 | $this->assertSame(14, Carbon::createFromDate(1975, 5, 21)->addWeeks(-1)->day); 103 | } 104 | 105 | public function testAddWeek() 106 | { 107 | $this->assertSame(28, Carbon::createFromDate(1975, 5, 21)->addWeek()->day); 108 | } 109 | 110 | public function testAddHoursPositive() 111 | { 112 | $this->assertSame(1, Carbon::createFromTime(0)->addHours(1)->hour); 113 | } 114 | public function testAddHoursZero() 115 | { 116 | $this->assertSame(0, Carbon::createFromTime(0)->addHours(0)->hour); 117 | } 118 | public function testAddHoursNegative() 119 | { 120 | $this->assertSame(23, Carbon::createFromTime(0)->addHours(-1)->hour); 121 | } 122 | 123 | public function testAddHour() 124 | { 125 | $this->assertSame(1, Carbon::createFromTime(0)->addHour()->hour); 126 | } 127 | 128 | public function testAddMinutesPositive() 129 | { 130 | $this->assertSame(1, Carbon::createFromTime(0, 0)->addMinutes(1)->minute); 131 | } 132 | public function testAddMinutesZero() 133 | { 134 | $this->assertSame(0, Carbon::createFromTime(0, 0)->addMinutes(0)->minute); 135 | } 136 | public function testAddMinutesNegative() 137 | { 138 | $this->assertSame(59, Carbon::createFromTime(0, 0)->addMinutes(-1)->minute); 139 | } 140 | 141 | public function testAddMinute() 142 | { 143 | $this->assertSame(1, Carbon::createFromTime(0, 0)->addMinute()->minute); 144 | } 145 | 146 | public function testAddSecondsPositive() 147 | { 148 | $this->assertSame(1, Carbon::createFromTime(0, 0, 0)->addSeconds(1)->second); 149 | } 150 | public function testAddSecondsZero() 151 | { 152 | $this->assertSame(0, Carbon::createFromTime(0, 0, 0)->addSeconds(0)->second); 153 | } 154 | public function testAddSecondsNegative() 155 | { 156 | $this->assertSame(59, Carbon::createFromTime(0, 0, 0)->addSeconds(-1)->second); 157 | } 158 | 159 | public function testAddSecond() 160 | { 161 | $this->assertSame(1, Carbon::createFromTime(0, 0, 0)->addSecond()->second); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /tests/TestingAidsTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class TestingAidsTest extends TestFixture 15 | { 16 | public function testTestingAidsWithTestNowNotSet() 17 | { 18 | Carbon::setTestNow(); 19 | 20 | $this->assertFalse(Carbon::hasTestNow()); 21 | $this->assertNull(Carbon::getTestNow()); 22 | } 23 | public function testTestingAidsWithTestNowSet() 24 | { 25 | $notNow = Carbon::yesterday(); 26 | Carbon::setTestNow($notNow); 27 | 28 | $this->assertTrue(Carbon::hasTestNow()); 29 | $this->assertSame($notNow, Carbon::getTestNow()); 30 | } 31 | 32 | public function testConstructorWithTestValueSet() 33 | { 34 | $notNow = Carbon::yesterday(); 35 | Carbon::setTestNow($notNow); 36 | 37 | $this->assertEquals($notNow, new Carbon()); 38 | $this->assertEquals($notNow, new Carbon(null)); 39 | $this->assertEquals($notNow, new Carbon('')); 40 | $this->assertEquals($notNow, new Carbon('now')); 41 | } 42 | 43 | public function testNowWithTestValueSet() 44 | { 45 | $notNow = Carbon::yesterday(); 46 | Carbon::setTestNow($notNow); 47 | 48 | $this->assertEquals($notNow, Carbon::now()); 49 | } 50 | 51 | public function testParseWithTestValueSet() 52 | { 53 | $notNow = Carbon::yesterday(); 54 | Carbon::setTestNow($notNow); 55 | 56 | $this->assertEquals($notNow, Carbon::parse()); 57 | $this->assertEquals($notNow, Carbon::parse(null)); 58 | $this->assertEquals($notNow, Carbon::parse('')); 59 | $this->assertEquals($notNow, Carbon::parse('now')); 60 | } 61 | 62 | public function testParseRelativeWithTestValueSet() 63 | { 64 | $notNow = Carbon::parse('2013-09-01 05:15:05'); 65 | Carbon::setTestNow($notNow); 66 | 67 | $this->assertEquals('2013-09-01 05:10:05', Carbon::parse('5 minutes ago')->toDateTimeString()); 68 | 69 | $this->assertEquals('2013-08-25 05:15:05', Carbon::parse('1 week ago')->toDateTimeString()); 70 | 71 | $this->assertEquals('2013-09-02 00:00:00', Carbon::parse('tomorrow')->toDateTimeString()); 72 | $this->assertEquals('2013-08-31 00:00:00', Carbon::parse('yesterday')->toDateTimeString()); 73 | 74 | $this->assertEquals('2013-09-02 05:15:05', Carbon::parse('+1 day')->toDateTimeString()); 75 | $this->assertEquals('2013-08-31 05:15:05', Carbon::parse('-1 day')->toDateTimeString()); 76 | 77 | $this->assertEquals('2013-09-02 00:00:00', Carbon::parse('next monday')->toDateTimeString()); 78 | $this->assertEquals('2013-09-03 00:00:00', Carbon::parse('next tuesday')->toDateTimeString()); 79 | $this->assertEquals('2013-09-04 00:00:00', Carbon::parse('next wednesday')->toDateTimeString()); 80 | $this->assertEquals('2013-09-05 00:00:00', Carbon::parse('next thursday')->toDateTimeString()); 81 | $this->assertEquals('2013-09-06 00:00:00', Carbon::parse('next friday')->toDateTimeString()); 82 | $this->assertEquals('2013-09-07 00:00:00', Carbon::parse('next saturday')->toDateTimeString()); 83 | $this->assertEquals('2013-09-08 00:00:00', Carbon::parse('next sunday')->toDateTimeString()); 84 | 85 | $this->assertEquals('2013-08-26 00:00:00', Carbon::parse('last monday')->toDateTimeString()); 86 | $this->assertEquals('2013-08-27 00:00:00', Carbon::parse('last tuesday')->toDateTimeString()); 87 | $this->assertEquals('2013-08-28 00:00:00', Carbon::parse('last wednesday')->toDateTimeString()); 88 | $this->assertEquals('2013-08-29 00:00:00', Carbon::parse('last thursday')->toDateTimeString()); 89 | $this->assertEquals('2013-08-30 00:00:00', Carbon::parse('last friday')->toDateTimeString()); 90 | $this->assertEquals('2013-08-31 00:00:00', Carbon::parse('last saturday')->toDateTimeString()); 91 | $this->assertEquals('2013-08-25 00:00:00', Carbon::parse('last sunday')->toDateTimeString()); 92 | 93 | $this->assertEquals('2013-09-02 00:00:00', Carbon::parse('this monday')->toDateTimeString()); 94 | $this->assertEquals('2013-09-03 00:00:00', Carbon::parse('this tuesday')->toDateTimeString()); 95 | $this->assertEquals('2013-09-04 00:00:00', Carbon::parse('this wednesday')->toDateTimeString()); 96 | $this->assertEquals('2013-09-05 00:00:00', Carbon::parse('this thursday')->toDateTimeString()); 97 | $this->assertEquals('2013-09-06 00:00:00', Carbon::parse('this friday')->toDateTimeString()); 98 | $this->assertEquals('2013-09-07 00:00:00', Carbon::parse('this saturday')->toDateTimeString()); 99 | $this->assertEquals('2013-09-01 00:00:00', Carbon::parse('this sunday')->toDateTimeString()); 100 | 101 | $this->assertEquals('2013-10-01 05:15:05', Carbon::parse('first day of next month')->toDateTimeString()); 102 | $this->assertEquals('2013-09-30 05:15:05', Carbon::parse('last day of this month')->toDateTimeString()); 103 | } 104 | 105 | public function testTimeZoneWithTestValueSet() 106 | { 107 | $notNow = Carbon::parse('2013-07-01 12:00:00', 'America/New_York'); 108 | Carbon::setTestNow($notNow); 109 | 110 | $this->assertEquals('2013-07-01T12:00:00-0400', Carbon::parse('now')->toISO8601String()); 111 | $this->assertEquals('2013-07-01T11:00:00-0500', Carbon::parse('now', 'America/Mexico_City')->toISO8601String()); 112 | $this->assertEquals('2013-07-01T09:00:00-0700', Carbon::parse('now', 'America/Vancouver')->toISO8601String()); 113 | } 114 | } 115 | -------------------------------------------------------------------------------- /tests/SettersTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class SettersTest extends TestFixture 15 | { 16 | public function testYearSetter() 17 | { 18 | $d = Carbon::now(); 19 | $d->year = 1995; 20 | $this->assertSame(1995, $d->year); 21 | } 22 | 23 | public function testMonthSetter() 24 | { 25 | $d = Carbon::now(); 26 | $d->month = 3; 27 | $this->assertSame(3, $d->month); 28 | } 29 | public function testMonthSetterWithWrap() 30 | { 31 | $d = Carbon::now(); 32 | $d->month = 13; 33 | $this->assertSame(1, $d->month); 34 | } 35 | 36 | public function testDaySetter() 37 | { 38 | $d = Carbon::now(); 39 | $d->day = 2; 40 | $this->assertSame(2, $d->day); 41 | } 42 | public function testDaySetterWithWrap() 43 | { 44 | $d = Carbon::createFromDate(2012, 8, 5); 45 | $d->day = 32; 46 | $this->assertSame(1, $d->day); 47 | } 48 | 49 | public function testHourSetter() 50 | { 51 | $d = Carbon::now(); 52 | $d->hour = 2; 53 | $this->assertSame(2, $d->hour); 54 | } 55 | public function testHourSetterWithWrap() 56 | { 57 | $d = Carbon::now(); 58 | $d->hour = 25; 59 | $this->assertSame(1, $d->hour); 60 | } 61 | 62 | public function testMinuteSetter() 63 | { 64 | $d = Carbon::now(); 65 | $d->minute = 2; 66 | $this->assertSame(2, $d->minute); 67 | } 68 | public function testMinuteSetterWithWrap() 69 | { 70 | $d = Carbon::now(); 71 | $d->minute = 65; 72 | $this->assertSame(5, $d->minute); 73 | } 74 | 75 | public function testSecondSetter() 76 | { 77 | $d = Carbon::now(); 78 | $d->second = 2; 79 | $this->assertSame(2, $d->second); 80 | } 81 | public function testSecondSetterWithWrap() 82 | { 83 | $d = Carbon::now(); 84 | $d->second = 65; 85 | $this->assertSame(5, $d->second); 86 | } 87 | 88 | public function testTimestampSetter() 89 | { 90 | $d = Carbon::now(); 91 | $d->timestamp = 10; 92 | $this->assertSame(10, $d->timestamp); 93 | 94 | $d->setTimestamp(11); 95 | $this->assertSame(11, $d->timestamp); 96 | } 97 | 98 | public function testSetTimezoneWithInvalidTimezone() 99 | { 100 | $this->setExpectedException('InvalidArgumentException'); 101 | $d = Carbon::now(); 102 | $d->setTimezone('sdf'); 103 | } 104 | public function testTimezoneWithInvalidTimezone() 105 | { 106 | $d = Carbon::now(); 107 | 108 | try { 109 | $d->timezone = 'sdf'; 110 | $this->fail('InvalidArgumentException was not been raised.'); 111 | } catch (InvalidArgumentException $expected) {} 112 | 113 | try { 114 | $d->timezone('sdf'); 115 | $this->fail('InvalidArgumentException was not been raised.'); 116 | } catch (InvalidArgumentException $expected) {} 117 | } 118 | public function testTzWithInvalidTimezone() 119 | { 120 | $d = Carbon::now(); 121 | 122 | try { 123 | $d->tz = 'sdf'; 124 | $this->fail('InvalidArgumentException was not been raised.'); 125 | } catch (InvalidArgumentException $expected) {} 126 | 127 | try { 128 | $d->tz('sdf'); 129 | $this->fail('InvalidArgumentException was not been raised.'); 130 | } catch (InvalidArgumentException $expected) {} 131 | } 132 | public function testSetTimezoneUsingString() 133 | { 134 | $d = Carbon::now(); 135 | $d->setTimezone('America/Toronto'); 136 | $this->assertSame('America/Toronto', $d->tzName); 137 | } 138 | public function testTimezoneUsingString() 139 | { 140 | $d = Carbon::now(); 141 | $d->timezone = 'America/Toronto'; 142 | $this->assertSame('America/Toronto', $d->tzName); 143 | 144 | $d->timezone('America/Vancouver'); 145 | $this->assertSame('America/Vancouver', $d->tzName); 146 | } 147 | public function testTzUsingString() 148 | { 149 | $d = Carbon::now(); 150 | $d->tz = 'America/Toronto'; 151 | $this->assertSame('America/Toronto', $d->tzName); 152 | 153 | $d->tz('America/Vancouver'); 154 | $this->assertSame('America/Vancouver', $d->tzName); 155 | } 156 | public function testSetTimezoneUsingDateTimeZone() 157 | { 158 | $d = Carbon::now(); 159 | $d->setTimezone(new \DateTimeZone('America/Toronto')); 160 | $this->assertSame('America/Toronto', $d->tzName); 161 | } 162 | public function testTimezoneUsingDateTimeZone() 163 | { 164 | $d = Carbon::now(); 165 | $d->timezone = new \DateTimeZone('America/Toronto'); 166 | $this->assertSame('America/Toronto', $d->tzName); 167 | 168 | $d->timezone(new \DateTimeZone('America/Vancouver')); 169 | $this->assertSame('America/Vancouver', $d->tzName); 170 | } 171 | public function testTzUsingDateTimeZone() 172 | { 173 | $d = Carbon::now(); 174 | $d->tz = new \DateTimeZone('America/Toronto'); 175 | $this->assertSame('America/Toronto', $d->tzName); 176 | 177 | $d->tz(new \DateTimeZone('America/Vancouver')); 178 | $this->assertSame('America/Vancouver', $d->tzName); 179 | } 180 | 181 | public function testInvalidSetter() 182 | { 183 | $this->setExpectedException('InvalidArgumentException'); 184 | $d = Carbon::now(); 185 | $d->doesNotExit = 'bb'; 186 | } 187 | } 188 | -------------------------------------------------------------------------------- /tests/ComparisonTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class ComparisonTest extends TestFixture 15 | { 16 | public function testEqualToTrue() 17 | { 18 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->eq(Carbon::createFromDate(2000, 1, 1))); 19 | } 20 | public function testEqualToFalse() 21 | { 22 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->eq(Carbon::createFromDate(2000, 1, 2))); 23 | } 24 | public function testEqualWithTimezoneTrue() 25 | { 26 | $this->assertTrue(Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto')->eq(Carbon::create(2000, 1, 1, 9, 0, 0, 'America/Vancouver'))); 27 | } 28 | public function testEqualWithTimezoneFalse() 29 | { 30 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1, 'America/Toronto')->eq(Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'))); 31 | } 32 | 33 | public function testNotEqualToTrue() 34 | { 35 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->ne(Carbon::createFromDate(2000, 1, 2))); 36 | } 37 | public function testNotEqualToFalse() 38 | { 39 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->ne(Carbon::createFromDate(2000, 1, 1))); 40 | } 41 | public function testNotEqualWithTimezone() 42 | { 43 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1, 'America/Toronto')->ne(Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'))); 44 | } 45 | 46 | public function testGreaterThanTrue() 47 | { 48 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gt(Carbon::createFromDate(1999, 12, 31))); 49 | } 50 | public function testGreaterThanFalse() 51 | { 52 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->gt(Carbon::createFromDate(2000, 1, 2))); 53 | } 54 | public function testGreaterThanWithTimezoneTrue() 55 | { 56 | $dt1 = Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto'); 57 | $dt2 = Carbon::create(2000, 1, 1, 8, 59, 59, 'America/Vancouver'); 58 | $this->assertTrue($dt1->gt($dt2)); 59 | } 60 | public function testGreaterThanWithTimezoneFalse() 61 | { 62 | $dt1 = Carbon::create(2000, 1, 1, 12, 0, 0, 'America/Toronto'); 63 | $dt2 = Carbon::create(2000, 1, 1, 9, 0, 1, 'America/Vancouver'); 64 | $this->assertFalse($dt1->gt($dt2)); 65 | } 66 | 67 | public function testGreaterThanOrEqualTrue() 68 | { 69 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(1999, 12, 31))); 70 | } 71 | public function testGreaterThanOrEqualTrueEqual() 72 | { 73 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(2000, 1, 1))); 74 | } 75 | public function testGreaterThanOrEqualFalse() 76 | { 77 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->gte(Carbon::createFromDate(2000, 1, 2))); 78 | } 79 | 80 | public function testLessThanTrue() 81 | { 82 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lt(Carbon::createFromDate(2000, 1, 2))); 83 | } 84 | public function testLessThanFalse() 85 | { 86 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->lt(Carbon::createFromDate(1999, 12, 31))); 87 | } 88 | 89 | public function testLessThanOrEqualTrue() 90 | { 91 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(2000, 1, 2))); 92 | } 93 | public function testLessThanOrEqualTrueEqual() 94 | { 95 | $this->assertTrue(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(2000, 1, 1))); 96 | } 97 | public function testLessThanOrEqualFalse() 98 | { 99 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->lte(Carbon::createFromDate(1999, 12, 31))); 100 | } 101 | 102 | public function testBetweenEqualTrue() 103 | { 104 | $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), true)); 105 | } 106 | public function testBetweenNotEqualTrue() 107 | { 108 | $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), false)); 109 | } 110 | public function testBetweenEqualFalse() 111 | { 112 | $this->assertFalse(Carbon::createFromDate(1999, 12, 31)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), true)); 113 | } 114 | public function testBetweenNotEqualFalse() 115 | { 116 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->between(Carbon::createFromDate(2000, 1, 1), Carbon::createFromDate(2000, 1, 31), false)); 117 | } 118 | public function testBetweenEqualSwitchTrue() 119 | { 120 | $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), true)); 121 | } 122 | public function testBetweenNotEqualSwitchTrue() 123 | { 124 | $this->assertTrue(Carbon::createFromDate(2000, 1, 15)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), false)); 125 | } 126 | public function testBetweenEqualSwitchFalse() 127 | { 128 | $this->assertFalse(Carbon::createFromDate(1999, 12, 31)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), true)); 129 | } 130 | public function testBetweenNotEqualSwitchFalse() 131 | { 132 | $this->assertFalse(Carbon::createFromDate(2000, 1, 1)->between(Carbon::createFromDate(2000, 1, 31), Carbon::createFromDate(2000, 1, 1), false)); 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /tests/GettersTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class GettersTest extends TestFixture 15 | { 16 | public function testGettersThrowExceptionOnUnknownGetter() 17 | { 18 | $this->setExpectedException('InvalidArgumentException'); 19 | Carbon::create(1234, 5, 6, 7, 8, 9)->sdfsdfss; 20 | } 21 | public function testYearGetter() 22 | { 23 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 24 | $this->assertSame(1234, $d->year); 25 | } 26 | public function testMonthGetter() 27 | { 28 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 29 | $this->assertSame(5, $d->month); 30 | } 31 | public function testDayGetter() 32 | { 33 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 34 | $this->assertSame(6, $d->day); 35 | } 36 | public function testHourGetter() 37 | { 38 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 39 | $this->assertSame(7, $d->hour); 40 | } 41 | public function testMinuteGetter() 42 | { 43 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 44 | $this->assertSame(8, $d->minute); 45 | } 46 | public function testSecondGetter() 47 | { 48 | $d = Carbon::create(1234, 5, 6, 7, 8, 9); 49 | $this->assertSame(9, $d->second); 50 | } 51 | public function testDayOfWeeGetter() 52 | { 53 | $d = Carbon::create(2012, 5, 7, 7, 8, 9); 54 | $this->assertSame(Carbon::MONDAY, $d->dayOfWeek); 55 | } 56 | public function testDayOfYearGetter() 57 | { 58 | $d = Carbon::createFromDate(2012, 5, 7); 59 | $this->assertSame(127, $d->dayOfYear); 60 | } 61 | public function testDaysInMonthGetter() 62 | { 63 | $d = Carbon::createFromDate(2012, 5, 7); 64 | $this->assertSame(31, $d->daysInMonth); 65 | } 66 | public function testTimestampGetter() 67 | { 68 | $d = Carbon::create(); 69 | $d->setTimezone('GMT'); 70 | $this->assertSame(0, $d->setDateTime(1970, 1, 1, 0, 0, 0)->timestamp); 71 | } 72 | 73 | public function testGetAge() 74 | { 75 | $d = Carbon::now(); 76 | $this->assertSame(0, $d->age); 77 | } 78 | public function testGetAgeWithRealAge() 79 | { 80 | $d = Carbon::createFromDate(1975, 5, 21); 81 | $age = intval(substr(date('Ymd') - date('Ymd', $d->timestamp), 0, -4)); 82 | 83 | $this->assertSame($age, $d->age); 84 | } 85 | 86 | public function testGetQuarterFirst() 87 | { 88 | $d = Carbon::createFromDate(2012, 1, 1); 89 | $this->assertSame(1, $d->quarter); 90 | } 91 | public function testGetQuarterFirstEnd() 92 | { 93 | $d = Carbon::createFromDate(2012, 3, 31); 94 | $this->assertSame(1, $d->quarter); 95 | } 96 | public function testGetQuarterSecond() 97 | { 98 | $d = Carbon::createFromDate(2012, 4, 1); 99 | $this->assertSame(2, $d->quarter); 100 | } 101 | public function testGetQuarterThird() 102 | { 103 | $d = Carbon::createFromDate(2012, 7, 1); 104 | $this->assertSame(3, $d->quarter); 105 | } 106 | public function testGetQuarterFourth() 107 | { 108 | $d = Carbon::createFromDate(2012, 10, 1); 109 | $this->assertSame(4, $d->quarter); 110 | } 111 | public function testGetQuarterFirstLast() 112 | { 113 | $d = Carbon::createFromDate(2012, 12, 31); 114 | $this->assertSame(4, $d->quarter); 115 | } 116 | 117 | public function testGetLocalTrue() 118 | { 119 | // Default timezone has been set to America/Toronto in TestFixture.php 120 | // @see : http://en.wikipedia.org/wiki/List_of_UTC_time_offsets 121 | $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->local); 122 | $this->assertTrue(Carbon::createFromDate(2012, 1, 1, 'America/New_York')->local); 123 | } 124 | public function testGetLocalFalse() 125 | { 126 | $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'UTC')->local); 127 | $this->assertFalse(Carbon::createFromDate(2012, 7, 1, 'Europe/London')->local); 128 | } 129 | 130 | public function testGetUtcFalse() 131 | { 132 | $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'America/Toronto')->utc); 133 | $this->assertFalse(Carbon::createFromDate(2013, 1, 1, 'Europe/Paris')->utc); 134 | } 135 | public function testGetUtcTrue() 136 | { 137 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Atlantic/Reykjavik')->utc); 138 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Lisbon')->utc); 139 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Casablanca')->utc); 140 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Africa/Dakar')->utc); 141 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/Dublin')->utc); 142 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'Europe/London')->utc); 143 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'UTC')->utc); 144 | $this->assertTrue(Carbon::createFromDate(2013, 1, 1, 'GMT')->utc); 145 | } 146 | 147 | public function testGetDstFalse() 148 | { 149 | $this->assertFalse(Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->dst); 150 | } 151 | public function testGetDstTrue() 152 | { 153 | $this->assertTrue(Carbon::createFromDate(2012, 7, 1, 'America/Toronto')->dst); 154 | } 155 | 156 | public function testOffsetForTorontoWithDST() 157 | { 158 | $this->assertSame(-18000, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offset); 159 | } 160 | public function testOffsetForTorontoNoDST() 161 | { 162 | $this->assertSame(-14400, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offset); 163 | } 164 | public function testOffsetForGMT() 165 | { 166 | $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offset); 167 | } 168 | public function testOffsetHoursForTorontoWithDST() 169 | { 170 | $this->assertSame(-5, Carbon::createFromDate(2012, 1, 1, 'America/Toronto')->offsetHours); 171 | } 172 | public function testOffsetHoursForTorontoNoDST() 173 | { 174 | $this->assertSame(-4, Carbon::createFromDate(2012, 6, 1, 'America/Toronto')->offsetHours); 175 | } 176 | public function testOffsetHoursForGMT() 177 | { 178 | $this->assertSame(0, Carbon::createFromDate(2012, 6, 1, 'GMT')->offsetHours); 179 | } 180 | 181 | public function testIsLeapYearTrue() 182 | { 183 | $this->assertTrue(Carbon::createFromDate(2012, 1, 1)->isLeapYear()); 184 | } 185 | public function testIsLeapYearFalse() 186 | { 187 | $this->assertFalse(Carbon::createFromDate(2011, 1, 1)->isLeapYear()); 188 | } 189 | 190 | public function testWeekOfYearFirstWeek() 191 | { 192 | $this->assertSame(52, Carbon::createFromDate(2012, 1, 1)->weekOfYear); 193 | $this->assertSame(1, Carbon::createFromDate(2012, 1, 2)->weekOfYear); 194 | } 195 | public function testWeekOfYearLastWeek() 196 | { 197 | $this->assertSame(52, Carbon::createFromDate(2012, 12, 30)->weekOfYear); 198 | $this->assertSame(1, Carbon::createFromDate(2012, 12, 31)->weekOfYear); 199 | } 200 | 201 | public function testGetTimezone() 202 | { 203 | $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 204 | $this->assertSame('America/Toronto', $dt->timezone->getName()); 205 | } 206 | public function testGetTz() 207 | { 208 | $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 209 | $this->assertSame('America/Toronto', $dt->tz->getName()); 210 | } 211 | public function testGetTimezoneName() 212 | { 213 | $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 214 | $this->assertSame('America/Toronto', $dt->timezoneName); 215 | } 216 | public function testGetTzName() 217 | { 218 | $dt = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 219 | $this->assertSame('America/Toronto', $dt->tzName); 220 | } 221 | 222 | public function testInvalidGetter() 223 | { 224 | $this->setExpectedException('InvalidArgumentException'); 225 | $d = Carbon::now(); 226 | $bb = $d->doesNotExit; 227 | } 228 | } 229 | -------------------------------------------------------------------------------- /tests/DayOfWeekModifiersTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class DayOfWeekModifiersTest extends TestFixture 15 | { 16 | public function testStartOfWeek() 17 | { 18 | $d = Carbon::create(1980, 8, 7, 12, 11, 9)->startOfWeek(); 19 | $this->assertCarbon($d, 1980, 8, 4, 0, 0, 0); 20 | } 21 | 22 | public function testStartOfWeekFromWeekStart() 23 | { 24 | $d = Carbon::createFromDate(1980, 8, 4)->startOfWeek(); 25 | $this->assertCarbon($d, 1980, 8, 4, 0, 0, 0); 26 | } 27 | 28 | public function testEndOfWeek() 29 | { 30 | $d = Carbon::create(1980, 8, 7, 11, 12, 13)->endOfWeek(); 31 | $this->assertCarbon($d, 1980, 8, 10, 23, 59, 59); 32 | } 33 | 34 | public function testEndOfWeekFromWeekEnd() 35 | { 36 | $d = Carbon::createFromDate(1980, 8, 9)->endOfWeek(); 37 | $this->assertCarbon($d, 1980, 8, 10, 23, 59, 59); 38 | } 39 | 40 | public function testNext() 41 | { 42 | $d = Carbon::createFromDate(1975, 5, 21)->next(); 43 | $this->assertCarbon($d, 1975, 5, 28, 0, 0, 0); 44 | } 45 | 46 | public function testNextMonday() 47 | { 48 | $d = Carbon::createFromDate(1975, 5, 21)->next(Carbon::MONDAY); 49 | $this->assertCarbon($d, 1975, 5, 26, 0, 0, 0); 50 | } 51 | 52 | public function testNextSaturday() 53 | { 54 | $d = Carbon::createFromDate(1975, 5, 21)->next(6); 55 | $this->assertCarbon($d, 1975, 5, 24, 0, 0, 0); 56 | } 57 | 58 | public function testNextTimestamp() 59 | { 60 | $d = Carbon::createFromDate(1975, 11, 14)->next(); 61 | $this->assertCarbon($d, 1975, 11, 21, 0, 0, 0); 62 | } 63 | 64 | public function testPrevious() 65 | { 66 | $d = Carbon::createFromDate(1975, 5, 21)->previous(); 67 | $this->assertCarbon($d, 1975, 5, 14, 0, 0, 0); 68 | } 69 | 70 | public function testPreviousMonday() 71 | { 72 | $d = Carbon::createFromDate(1975, 5, 21)->previous(Carbon::MONDAY); 73 | $this->assertCarbon($d, 1975, 5, 19, 0, 0, 0); 74 | } 75 | 76 | public function testPreviousSaturday() 77 | { 78 | $d = Carbon::createFromDate(1975, 5, 21)->previous(6); 79 | $this->assertCarbon($d, 1975, 5, 17, 0, 0, 0); 80 | } 81 | 82 | public function testPreviousTimestamp() 83 | { 84 | $d = Carbon::createFromDate(1975, 11, 28)->previous(); 85 | $this->assertCarbon($d, 1975, 11, 21, 0, 0, 0); 86 | } 87 | 88 | public function testFirstDayOfMonth() 89 | { 90 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth(); 91 | $this->assertCarbon($d, 1975, 11, 1, 0, 0, 0); 92 | } 93 | 94 | public function testFirstWednesdayOfMonth() 95 | { 96 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth(Carbon::WEDNESDAY); 97 | $this->assertCarbon($d, 1975, 11, 5, 0, 0, 0); 98 | } 99 | 100 | public function testFirstFridayOfMonth() 101 | { 102 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfMonth(5); 103 | $this->assertCarbon($d, 1975, 11, 7, 0, 0, 0); 104 | } 105 | 106 | public function testLastDayOfMonth() 107 | { 108 | $d = Carbon::createFromDate(1975, 12, 5)->lastOfMonth(); 109 | $this->assertCarbon($d, 1975, 12, 31, 0, 0, 0); 110 | } 111 | 112 | public function testLastTuesdayOfMonth() 113 | { 114 | $d = Carbon::createFromDate(1975, 12, 1)->lastOfMonth(Carbon::TUESDAY); 115 | $this->assertCarbon($d, 1975, 12, 30, 0, 0, 0); 116 | } 117 | 118 | public function testLastFridayOfMonth() 119 | { 120 | $d = Carbon::createFromDate(1975, 12, 5)->lastOfMonth(5); 121 | $this->assertCarbon($d, 1975, 12, 26, 0, 0, 0); 122 | } 123 | 124 | public function testNthOfMonthOutsideScope() 125 | { 126 | $this->assertFalse(Carbon::createFromDate(1975, 12, 5)->nthOfMonth(6, Carbon::MONDAY)); 127 | } 128 | 129 | public function testNthOfMonthOutsideYear() 130 | { 131 | $this->assertFalse(Carbon::createFromDate(1975, 12, 5)->nthOfMonth(55, Carbon::MONDAY)); 132 | } 133 | 134 | public function test2ndMondayOfMonth() 135 | { 136 | $d = Carbon::createFromDate(1975, 12, 5)->nthOfMonth(2, Carbon::MONDAY); 137 | $this->assertCarbon($d, 1975, 12, 8, 0, 0, 0); 138 | } 139 | 140 | public function test3rdWednesdayOfMonth() 141 | { 142 | $d = Carbon::createFromDate(1975, 12, 5)->nthOfMonth(3, 3); 143 | $this->assertCarbon($d, 1975, 12, 17, 0, 0, 0); 144 | } 145 | 146 | public function testFirstDayOfQuarter() 147 | { 148 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter(); 149 | $this->assertCarbon($d, 1975, 10, 1, 0, 0, 0); 150 | } 151 | 152 | public function testFirstWednesdayOfQuarter() 153 | { 154 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter(Carbon::WEDNESDAY); 155 | $this->assertCarbon($d, 1975, 10, 1, 0, 0, 0); 156 | } 157 | 158 | public function testFirstFridayOfQuarter() 159 | { 160 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfQuarter(5); 161 | $this->assertCarbon($d, 1975, 10, 3, 0, 0, 0); 162 | } 163 | 164 | public function testLastDayOfQuarter() 165 | { 166 | $d = Carbon::createFromDate(1975, 8, 5)->lastOfQuarter(); 167 | $this->assertCarbon($d, 1975, 9, 30, 0, 0, 0); 168 | } 169 | 170 | public function testLastTuesdayOfQuarter() 171 | { 172 | $d = Carbon::createFromDate(1975, 8, 1)->lastOfQuarter(Carbon::TUESDAY); 173 | $this->assertCarbon($d, 1975, 9, 30, 0, 0, 0); 174 | } 175 | 176 | public function testLastFridayOfQuarter() 177 | { 178 | $d = Carbon::createFromDate(1975, 7, 5)->lastOfQuarter(5); 179 | $this->assertCarbon($d, 1975, 9, 26, 0, 0, 0); 180 | } 181 | 182 | public function testNthOfQuarterOutsideScope() 183 | { 184 | $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfQuarter(20, Carbon::MONDAY)); 185 | } 186 | 187 | public function testNthOfQuarterOutsideYear() 188 | { 189 | $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfQuarter(55, Carbon::MONDAY)); 190 | } 191 | 192 | public function test2ndMondayOfQuarter() 193 | { 194 | $d = Carbon::createFromDate(1975, 8, 5)->nthOfQuarter(2, Carbon::MONDAY); 195 | $this->assertCarbon($d, 1975, 7, 14, 0, 0, 0); 196 | } 197 | 198 | public function test3rdWednesdayOfQuarter() 199 | { 200 | $d = Carbon::createFromDate(1975, 8, 5)->nthOfQuarter(3, 3); 201 | $this->assertCarbon($d, 1975, 7, 16, 0, 0, 0); 202 | } 203 | 204 | public function testFirstDayOfYear() 205 | { 206 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear(); 207 | $this->assertCarbon($d, 1975, 1, 1, 0, 0, 0); 208 | } 209 | 210 | public function testFirstWednesdayOfYear() 211 | { 212 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear(Carbon::WEDNESDAY); 213 | $this->assertCarbon($d, 1975, 1, 1, 0, 0, 0); 214 | } 215 | 216 | public function testFirstFridayOfYear() 217 | { 218 | $d = Carbon::createFromDate(1975, 11, 21)->firstOfYear(5); 219 | $this->assertCarbon($d, 1975, 1, 3, 0, 0, 0); 220 | } 221 | 222 | public function testLastDayOfYear() 223 | { 224 | $d = Carbon::createFromDate(1975, 8, 5)->lastOfYear(); 225 | $this->assertCarbon($d, 1975, 12, 31, 0, 0, 0); 226 | } 227 | 228 | public function testLastTuesdayOfYear() 229 | { 230 | $d = Carbon::createFromDate(1975, 8, 1)->lastOfYear(Carbon::TUESDAY); 231 | $this->assertCarbon($d, 1975, 12, 30, 0, 0, 0); 232 | } 233 | 234 | public function testLastFridayOfYear() 235 | { 236 | $d = Carbon::createFromDate(1975, 7, 5)->lastOfYear(5); 237 | $this->assertCarbon($d, 1975, 12, 26, 0, 0, 0); 238 | } 239 | 240 | public function testNthOfYearOutsideScope() 241 | { 242 | $this->assertFalse(Carbon::createFromDate(1975, 1, 5)->nthOfYear(55, Carbon::MONDAY)); 243 | } 244 | 245 | public function test2ndMondayOfYear() 246 | { 247 | $d = Carbon::createFromDate(1975, 8, 5)->nthOfYear(2, Carbon::MONDAY); 248 | $this->assertCarbon($d, 1975, 1, 13, 0, 0, 0); 249 | } 250 | 251 | public function test3rdWednesdayOfYear() 252 | { 253 | $d = Carbon::createFromDate(1975, 8, 5)->nthOfYear(3, 3); 254 | $this->assertCarbon($d, 1975, 1, 15, 0, 0, 0); 255 | } 256 | } 257 | -------------------------------------------------------------------------------- /tests/DiffTest.php: -------------------------------------------------------------------------------- 1 | 7 | * 8 | * For the full copyright and license information, please view the LICENSE 9 | * file that was distributed with this source code. 10 | */ 11 | 12 | use Carbon\Carbon; 13 | 14 | class DiffTest extends TestFixture 15 | { 16 | public function testDiffInYearsPositive() 17 | { 18 | $dt = Carbon::createFromDate(2000, 1, 1); 19 | $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear())); 20 | } 21 | public function testDiffInYearsNegativeWithSign() 22 | { 23 | $dt = Carbon::createFromDate(2000, 1, 1); 24 | $this->assertSame(-1, $dt->diffInYears($dt->copy()->subYear(), false)); 25 | } 26 | public function testDiffInYearsNegativeNoSign() 27 | { 28 | $dt = Carbon::createFromDate(2000, 1, 1); 29 | $this->assertSame(1, $dt->diffInYears($dt->copy()->subYear())); 30 | } 31 | public function testDiffInYearsVsDefaultNow() 32 | { 33 | $this->assertSame(1, Carbon::now()->subYear()->diffInYears()); 34 | } 35 | public function testDiffInYearsEnsureIsTruncated() 36 | { 37 | $dt = Carbon::createFromDate(2000, 1, 1); 38 | $this->assertSame(1, $dt->diffInYears($dt->copy()->addYear()->addMonths(7))); 39 | } 40 | 41 | public function testDiffInMonthsPositive() 42 | { 43 | $dt = Carbon::createFromDate(2000, 1, 1); 44 | $this->assertSame(13, $dt->diffInMonths($dt->copy()->addYear()->addMonth())); 45 | } 46 | public function testDiffInMonthsNegativeWithSign() 47 | { 48 | $dt = Carbon::createFromDate(2000, 1, 1); 49 | $this->assertSame(-11, $dt->diffInMonths($dt->copy()->subYear()->addMonth(), false)); 50 | } 51 | public function testDiffInMonthsNegativeNoSign() 52 | { 53 | $dt = Carbon::createFromDate(2000, 1, 1); 54 | $this->assertSame(11, $dt->diffInMonths($dt->copy()->subYear()->addMonth())); 55 | } 56 | public function testDiffInMonthsVsDefaultNow() 57 | { 58 | $this->assertSame(12, Carbon::now()->subYear()->diffInMonths()); 59 | } 60 | public function testDiffInMonthsEnsureIsTruncated() 61 | { 62 | $dt = Carbon::createFromDate(2000, 1, 1); 63 | $this->assertSame(1, $dt->diffInMonths($dt->copy()->addMonth()->addDays(16))); 64 | } 65 | 66 | public function testDiffInDaysPositive() 67 | { 68 | $dt = Carbon::createFromDate(2000, 1, 1); 69 | $this->assertSame(366, $dt->diffInDays($dt->copy()->addYear())); 70 | } 71 | public function testDiffInDaysNegativeWithSign() 72 | { 73 | $dt = Carbon::createFromDate(2000, 1, 1); 74 | $this->assertSame(-365, $dt->diffInDays($dt->copy()->subYear(), false)); 75 | } 76 | public function testDiffInDaysNegativeNoSign() 77 | { 78 | $dt = Carbon::createFromDate(2000, 1, 1); 79 | $this->assertSame(365, $dt->diffInDays($dt->copy()->subYear())); 80 | } 81 | public function testDiffInDaysVsDefaultNow() 82 | { 83 | $this->assertSame(7, Carbon::now()->subWeek()->diffInDays()); 84 | } 85 | public function testDiffInDaysEnsureIsTruncated() 86 | { 87 | $dt = Carbon::createFromDate(2000, 1, 1); 88 | $this->assertSame(1, $dt->diffInDays($dt->copy()->addDay()->addHours(13))); 89 | } 90 | 91 | public function testDiffInHoursPositive() 92 | { 93 | $dt = Carbon::createFromDate(2000, 1, 1); 94 | $this->assertSame(26, $dt->diffInHours($dt->copy()->addDay()->addHours(2))); 95 | } 96 | public function testDiffInHoursNegativeWithSign() 97 | { 98 | $dt = Carbon::createFromDate(2000, 1, 1); 99 | $this->assertSame(-22, $dt->diffInHours($dt->copy()->subDay()->addHours(2), false)); 100 | } 101 | public function testDiffInHoursNegativeNoSign() 102 | { 103 | $dt = Carbon::createFromDate(2000, 1, 1); 104 | $this->assertSame(22, $dt->diffInHours($dt->copy()->subDay()->addHours(2))); 105 | } 106 | public function testDiffInHoursVsDefaultNow() 107 | { 108 | $this->assertSame(48, Carbon::now()->subDays(2)->diffInHours()); 109 | } 110 | public function testDiffInHoursEnsureIsTruncated() 111 | { 112 | $dt = Carbon::createFromDate(2000, 1, 1); 113 | $this->assertSame(1, $dt->diffInHours($dt->copy()->addHour()->addMinutes(31))); 114 | } 115 | 116 | public function testDiffInMinutesPositive() 117 | { 118 | $dt = Carbon::createFromDate(2000, 1, 1); 119 | $this->assertSame(62, $dt->diffInMinutes($dt->copy()->addHour()->addMinutes(2))); 120 | } 121 | public function testDiffInMinutesPositiveAlot() 122 | { 123 | $dt = Carbon::createFromDate(2000, 1, 1); 124 | $this->assertSame(1502, $dt->diffInMinutes($dt->copy()->addHours(25)->addMinutes(2))); 125 | } 126 | public function testDiffInMinutesNegativeWithSign() 127 | { 128 | $dt = Carbon::createFromDate(2000, 1, 1); 129 | $this->assertSame(-58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2), false)); 130 | } 131 | public function testDiffInMinutesNegativeNoSign() 132 | { 133 | $dt = Carbon::createFromDate(2000, 1, 1); 134 | $this->assertSame(58, $dt->diffInMinutes($dt->copy()->subHour()->addMinutes(2))); 135 | } 136 | public function testDiffInMinutesVsDefaultNow() 137 | { 138 | $this->assertSame(60, Carbon::now()->subHour()->diffInMinutes()); 139 | } 140 | public function testDiffInMinutesEnsureIsTruncated() 141 | { 142 | $dt = Carbon::createFromDate(2000, 1, 1); 143 | $this->assertSame(1, $dt->diffInMinutes($dt->copy()->addMinute()->addSeconds(31))); 144 | } 145 | 146 | public function testDiffInSecondsPositive() 147 | { 148 | $dt = Carbon::createFromDate(2000, 1, 1); 149 | $this->assertSame(62, $dt->diffInSeconds($dt->copy()->addMinute()->addSeconds(2))); 150 | } 151 | public function testDiffInSecondsPositiveAlot() 152 | { 153 | $dt = Carbon::createFromDate(2000, 1, 1); 154 | $this->assertSame(7202, $dt->diffInSeconds($dt->copy()->addHours(2)->addSeconds(2))); 155 | } 156 | public function testDiffInSecondsNegativeWithSign() 157 | { 158 | $dt = Carbon::createFromDate(2000, 1, 1); 159 | $this->assertSame(-58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2), false)); 160 | } 161 | public function testDiffInSecondsNegativeNoSign() 162 | { 163 | $dt = Carbon::createFromDate(2000, 1, 1); 164 | $this->assertSame(58, $dt->diffInSeconds($dt->copy()->subMinute()->addSeconds(2))); 165 | } 166 | public function testDiffInSecondsVsDefaultNow() 167 | { 168 | $this->assertSame(3600, Carbon::now()->subHour()->diffInSeconds()); 169 | } 170 | public function testDiffInSecondsEnsureIsTruncated() 171 | { 172 | $dt = Carbon::createFromDate(2000, 1, 1); 173 | $this->assertSame(1, $dt->diffInSeconds($dt->copy()->addSeconds(1.9))); 174 | } 175 | 176 | public function testDiffInSecondsWithTimezones() 177 | { 178 | $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 179 | $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'); 180 | $this->assertSame(3*60*60, $dtOttawa->diffInSeconds($dtVancouver)); 181 | } 182 | public function testDiffInSecondsWithTimezonesAndVsDefault() 183 | { 184 | $dt = Carbon::now('America/Vancouver'); 185 | $this->assertSame(0, $dt->diffInSeconds()); 186 | } 187 | 188 | public function testDiffForHumansNowAndSecond() 189 | { 190 | $d = Carbon::now(); 191 | $this->assertSame('1 second ago', $d->diffForHumans()); 192 | } 193 | public function testDiffForHumansNowAndSecondWithTimezone() 194 | { 195 | $d = Carbon::now('America/Vancouver'); 196 | $this->assertSame('1 second ago', $d->diffForHumans()); 197 | } 198 | public function testDiffForHumansNowAndSeconds() 199 | { 200 | $d = Carbon::now()->subSeconds(2); 201 | $this->assertSame('2 seconds ago', $d->diffForHumans()); 202 | } 203 | public function testDiffForHumansNowAndNearlyMinute() 204 | { 205 | $d = Carbon::now()->subSeconds(59); 206 | $this->assertSame('59 seconds ago', $d->diffForHumans()); 207 | } 208 | public function testDiffForHumansNowAndMinute() 209 | { 210 | $d = Carbon::now()->subMinute(); 211 | $this->assertSame('1 minute ago', $d->diffForHumans()); 212 | } 213 | public function testDiffForHumansNowAndMinutes() 214 | { 215 | $d = Carbon::now()->subMinutes(2); 216 | $this->assertSame('2 minutes ago', $d->diffForHumans()); 217 | } 218 | public function testDiffForHumansNowAndNearlyHour() 219 | { 220 | $d = Carbon::now()->subMinutes(59); 221 | $this->assertSame('59 minutes ago', $d->diffForHumans()); 222 | } 223 | public function testDiffForHumansNowAndHour() 224 | { 225 | $d = Carbon::now()->subHour(); 226 | $this->assertSame('1 hour ago', $d->diffForHumans()); 227 | } 228 | public function testDiffForHumansNowAndHours() 229 | { 230 | $d = Carbon::now()->subHours(2); 231 | $this->assertSame('2 hours ago', $d->diffForHumans()); 232 | } 233 | public function testDiffForHumansNowAndNearlyDay() 234 | { 235 | $d = Carbon::now()->subHours(23); 236 | $this->assertSame('23 hours ago', $d->diffForHumans()); 237 | } 238 | public function testDiffForHumansNowAndDay() 239 | { 240 | $d = Carbon::now()->subDay(); 241 | $this->assertSame('1 day ago', $d->diffForHumans()); 242 | } 243 | public function testDiffForHumansNowAndDays() 244 | { 245 | $d = Carbon::now()->subDays(2); 246 | $this->assertSame('2 days ago', $d->diffForHumans()); 247 | } 248 | public function testDiffForHumansNowAndNearlyWeek() 249 | { 250 | $d = Carbon::now()->subDays(6); 251 | $this->assertSame('6 days ago', $d->diffForHumans()); 252 | } 253 | public function testDiffForHumansNowAndWeek() 254 | { 255 | $d = Carbon::now()->subWeek(); 256 | $this->assertSame('1 week ago', $d->diffForHumans()); 257 | } 258 | public function testDiffForHumansNowAndWeeks() 259 | { 260 | $d = Carbon::now()->subWeeks(2); 261 | $this->assertSame('2 weeks ago', $d->diffForHumans()); 262 | } 263 | public function testDiffForHumansNowAndNearlyMonth() 264 | { 265 | $d = Carbon::now()->subWeeks(3); 266 | $this->assertSame('3 weeks ago', $d->diffForHumans()); 267 | } 268 | public function testDiffForHumansNowAndMonth() 269 | { 270 | $d = Carbon::now()->subWeeks(4); 271 | $this->assertSame('1 month ago', $d->diffForHumans()); 272 | $d = Carbon::now()->subMonth(); 273 | $this->assertSame('1 month ago', $d->diffForHumans()); 274 | } 275 | public function testDiffForHumansNowAndMonths() 276 | { 277 | $d = Carbon::now()->subMonths(2); 278 | $this->assertSame('2 months ago', $d->diffForHumans()); 279 | } 280 | public function testDiffForHumansNowAndNearlyYear() 281 | { 282 | $d = Carbon::now()->subMonths(11); 283 | $this->assertSame('11 months ago', $d->diffForHumans()); 284 | } 285 | public function testDiffForHumansNowAndYear() 286 | { 287 | $d = Carbon::now()->subYear(); 288 | $this->assertSame('1 year ago', $d->diffForHumans()); 289 | } 290 | public function testDiffForHumansNowAndYears() 291 | { 292 | $d = Carbon::now()->subYears(2); 293 | $this->assertSame('2 years ago', $d->diffForHumans()); 294 | } 295 | 296 | public function testDiffForHumansNowAndFutureSecond() 297 | { 298 | $d = Carbon::now()->addSecond(); 299 | $this->assertSame('1 second from now', $d->diffForHumans()); 300 | } 301 | public function testDiffForHumansNowAndFutureSeconds() 302 | { 303 | $d = Carbon::now()->addSeconds(2); 304 | $this->assertSame('2 seconds from now', $d->diffForHumans()); 305 | } 306 | public function testDiffForHumansNowAndNearlyFutureMinute() 307 | { 308 | $d = Carbon::now()->addSeconds(59); 309 | $this->assertSame('59 seconds from now', $d->diffForHumans()); 310 | } 311 | public function testDiffForHumansNowAndFutureMinute() 312 | { 313 | $d = Carbon::now()->addMinute(); 314 | $this->assertSame('1 minute from now', $d->diffForHumans()); 315 | } 316 | public function testDiffForHumansNowAndFutureMinutes() 317 | { 318 | $d = Carbon::now()->addMinutes(2); 319 | $this->assertSame('2 minutes from now', $d->diffForHumans()); 320 | } 321 | public function testDiffForHumansNowAndNearlyFutureHour() 322 | { 323 | $d = Carbon::now()->addMinutes(59); 324 | $this->assertSame('59 minutes from now', $d->diffForHumans()); 325 | } 326 | public function testDiffForHumansNowAndFutureHour() 327 | { 328 | $d = Carbon::now()->addHour(); 329 | $this->assertSame('1 hour from now', $d->diffForHumans()); 330 | } 331 | public function testDiffForHumansNowAndFutureHours() 332 | { 333 | $d = Carbon::now()->addHours(2); 334 | $this->assertSame('2 hours from now', $d->diffForHumans()); 335 | } 336 | public function testDiffForHumansNowAndNearlyFutureDay() 337 | { 338 | $d = Carbon::now()->addHours(23); 339 | $this->assertSame('23 hours from now', $d->diffForHumans()); 340 | } 341 | public function testDiffForHumansNowAndFutureDay() 342 | { 343 | $d = Carbon::now()->addDay(); 344 | $this->assertSame('1 day from now', $d->diffForHumans()); 345 | } 346 | public function testDiffForHumansNowAndFutureDays() 347 | { 348 | $d = Carbon::now()->addDays(2); 349 | $this->assertSame('2 days from now', $d->diffForHumans()); 350 | } 351 | public function testDiffForHumansNowAndNearlyFutureWeek() 352 | { 353 | $d = Carbon::now()->addDays(6); 354 | $this->assertSame('6 days from now', $d->diffForHumans()); 355 | } 356 | public function testDiffForHumansNowAndFutureWeek() 357 | { 358 | $d = Carbon::now()->addWeek(); 359 | $this->assertSame('1 week from now', $d->diffForHumans()); 360 | } 361 | public function testDiffForHumansNowAndFutureWeeks() 362 | { 363 | $d = Carbon::now()->addWeeks(2); 364 | $this->assertSame('2 weeks from now', $d->diffForHumans()); 365 | } 366 | public function testDiffForHumansNowAndNearlyFutureMonth() 367 | { 368 | $d = Carbon::now()->addWeeks(3); 369 | $this->assertSame('3 weeks from now', $d->diffForHumans()); 370 | } 371 | public function testDiffForHumansNowAndFutureMonth() 372 | { 373 | $d = Carbon::now()->addWeeks(4); 374 | $this->assertSame('1 month from now', $d->diffForHumans()); 375 | $d = Carbon::now()->addMonth(); 376 | $this->assertSame('1 month from now', $d->diffForHumans()); 377 | } 378 | public function testDiffForHumansNowAndFutureMonths() 379 | { 380 | $d = Carbon::now()->addMonths(2); 381 | $this->assertSame('2 months from now', $d->diffForHumans()); 382 | } 383 | public function testDiffForHumansNowAndNearlyFutureYear() 384 | { 385 | $d = Carbon::now()->addMonths(11); 386 | $this->assertSame('11 months from now', $d->diffForHumans()); 387 | } 388 | public function testDiffForHumansNowAndFutureYear() 389 | { 390 | $d = Carbon::now()->addYear(); 391 | $this->assertSame('1 year from now', $d->diffForHumans()); 392 | } 393 | public function testDiffForHumansNowAndFutureYears() 394 | { 395 | $d = Carbon::now()->addYears(2); 396 | $this->assertSame('2 years from now', $d->diffForHumans()); 397 | } 398 | 399 | public function testDiffForHumansOtherAndSecond() 400 | { 401 | $d = Carbon::now()->addSecond(); 402 | $this->assertSame('1 second before', Carbon::now()->diffForHumans($d)); 403 | } 404 | public function testDiffForHumansOtherAndSeconds() 405 | { 406 | $d = Carbon::now()->addSeconds(2); 407 | $this->assertSame('2 seconds before', Carbon::now()->diffForHumans($d)); 408 | } 409 | public function testDiffForHumansOtherAndNearlyMinute() 410 | { 411 | $d = Carbon::now()->addSeconds(59); 412 | $this->assertSame('59 seconds before', Carbon::now()->diffForHumans($d)); 413 | } 414 | public function testDiffForHumansOtherAndMinute() 415 | { 416 | $d = Carbon::now()->addMinute(); 417 | $this->assertSame('1 minute before', Carbon::now()->diffForHumans($d)); 418 | } 419 | public function testDiffForHumansOtherAndMinutes() 420 | { 421 | $d = Carbon::now()->addMinutes(2); 422 | $this->assertSame('2 minutes before', Carbon::now()->diffForHumans($d)); 423 | } 424 | public function testDiffForHumansOtherAndNearlyHour() 425 | { 426 | $d = Carbon::now()->addMinutes(59); 427 | $this->assertSame('59 minutes before', Carbon::now()->diffForHumans($d)); 428 | } 429 | public function testDiffForHumansOtherAndHour() 430 | { 431 | $d = Carbon::now()->addHour(); 432 | $this->assertSame('1 hour before', Carbon::now()->diffForHumans($d)); 433 | } 434 | public function testDiffForHumansOtherAndHours() 435 | { 436 | $d = Carbon::now()->addHours(2); 437 | $this->assertSame('2 hours before', Carbon::now()->diffForHumans($d)); 438 | } 439 | public function testDiffForHumansOtherAndNearlyDay() 440 | { 441 | $d = Carbon::now()->addHours(23); 442 | $this->assertSame('23 hours before', Carbon::now()->diffForHumans($d)); 443 | } 444 | public function testDiffForHumansOtherAndDay() 445 | { 446 | $d = Carbon::now()->addDay(); 447 | $this->assertSame('1 day before', Carbon::now()->diffForHumans($d)); 448 | } 449 | public function testDiffForHumansOtherAndDays() 450 | { 451 | $d = Carbon::now()->addDays(2); 452 | $this->assertSame('2 days before', Carbon::now()->diffForHumans($d)); 453 | } 454 | public function testDiffForHumansOtherAndNearlyWeek() 455 | { 456 | $d = Carbon::now()->addDays(6); 457 | $this->assertSame('6 days before', Carbon::now()->diffForHumans($d)); 458 | } 459 | public function testDiffForHumansOtherAndWeek() 460 | { 461 | $d = Carbon::now()->addWeek(); 462 | $this->assertSame('1 week before', Carbon::now()->diffForHumans($d)); 463 | } 464 | public function testDiffForHumansOtherAndWeeks() 465 | { 466 | $d = Carbon::now()->addWeeks(2); 467 | $this->assertSame('2 weeks before', Carbon::now()->diffForHumans($d)); 468 | } 469 | public function testDiffForHumansOtherAndNearlyMonth() 470 | { 471 | $d = Carbon::now()->addWeeks(3); 472 | $this->assertSame('3 weeks before', Carbon::now()->diffForHumans($d)); 473 | } 474 | public function testDiffForHumansOtherAndMonth() 475 | { 476 | $d = Carbon::now()->addWeeks(4); 477 | $this->assertSame('1 month before', Carbon::now()->diffForHumans($d)); 478 | $d = Carbon::now()->addMonth(); 479 | $this->assertSame('1 month before', Carbon::now()->diffForHumans($d)); 480 | } 481 | public function testDiffForHumansOtherAndMonths() 482 | { 483 | $d = Carbon::now()->addMonths(2); 484 | $this->assertSame('2 months before', Carbon::now()->diffForHumans($d)); 485 | } 486 | public function testDiffForHumansOtherAndNearlyYear() 487 | { 488 | $d = Carbon::now()->addMonths(11); 489 | $this->assertSame('11 months before', Carbon::now()->diffForHumans($d)); 490 | } 491 | public function testDiffForHumansOtherAndYear() 492 | { 493 | $d = Carbon::now()->addYear(); 494 | $this->assertSame('1 year before', Carbon::now()->diffForHumans($d)); 495 | } 496 | public function testDiffForHumansOtherAndYears() 497 | { 498 | $d = Carbon::now()->addYears(2); 499 | $this->assertSame('2 years before', Carbon::now()->diffForHumans($d)); 500 | } 501 | 502 | public function testDiffForHumansOtherAndFutureSecond() 503 | { 504 | $d = Carbon::now()->subSecond(); 505 | $this->assertSame('1 second after', Carbon::now()->diffForHumans($d)); 506 | } 507 | public function testDiffForHumansOtherAndFutureSeconds() 508 | { 509 | $d = Carbon::now()->subSeconds(2); 510 | $this->assertSame('2 seconds after', Carbon::now()->diffForHumans($d)); 511 | } 512 | public function testDiffForHumansOtherAndNearlyFutureMinute() 513 | { 514 | $d = Carbon::now()->subSeconds(59); 515 | $this->assertSame('59 seconds after', Carbon::now()->diffForHumans($d)); 516 | } 517 | public function testDiffForHumansOtherAndFutureMinute() 518 | { 519 | $d = Carbon::now()->subMinute(); 520 | $this->assertSame('1 minute after', Carbon::now()->diffForHumans($d)); 521 | } 522 | public function testDiffForHumansOtherAndFutureMinutes() 523 | { 524 | $d = Carbon::now()->subMinutes(2); 525 | $this->assertSame('2 minutes after', Carbon::now()->diffForHumans($d)); 526 | } 527 | public function testDiffForHumansOtherAndNearlyFutureHour() 528 | { 529 | $d = Carbon::now()->subMinutes(59); 530 | $this->assertSame('59 minutes after', Carbon::now()->diffForHumans($d)); 531 | } 532 | public function testDiffForHumansOtherAndFutureHour() 533 | { 534 | $d = Carbon::now()->subHour(); 535 | $this->assertSame('1 hour after', Carbon::now()->diffForHumans($d)); 536 | } 537 | public function testDiffForHumansOtherAndFutureHours() 538 | { 539 | $d = Carbon::now()->subHours(2); 540 | $this->assertSame('2 hours after', Carbon::now()->diffForHumans($d)); 541 | } 542 | public function testDiffForHumansOtherAndNearlyFutureDay() 543 | { 544 | $d = Carbon::now()->subHours(23); 545 | $this->assertSame('23 hours after', Carbon::now()->diffForHumans($d)); 546 | } 547 | public function testDiffForHumansOtherAndFutureDay() 548 | { 549 | $d = Carbon::now()->subDay(); 550 | $this->assertSame('1 day after', Carbon::now()->diffForHumans($d)); 551 | } 552 | public function testDiffForHumansOtherAndFutureDays() 553 | { 554 | $d = Carbon::now()->subDays(2); 555 | $this->assertSame('2 days after', Carbon::now()->diffForHumans($d)); 556 | } 557 | public function testDiffForHumansOtherAndNearlyFutureWeek() 558 | { 559 | $d = Carbon::now()->subDays(6); 560 | $this->assertSame('6 days after', Carbon::now()->diffForHumans($d)); 561 | } 562 | public function testDiffForHumansOtherAndFutureWeek() 563 | { 564 | $d = Carbon::now()->subWeek(); 565 | $this->assertSame('1 week after', Carbon::now()->diffForHumans($d)); 566 | } 567 | public function testDiffForHumansOtherAndFutureWeeks() 568 | { 569 | $d = Carbon::now()->subWeeks(2); 570 | $this->assertSame('2 weeks after', Carbon::now()->diffForHumans($d)); 571 | } 572 | public function testDiffForHumansOtherAndNearlyFutureMonth() 573 | { 574 | $d = Carbon::now()->subWeeks(3); 575 | $this->assertSame('3 weeks after', Carbon::now()->diffForHumans($d)); 576 | } 577 | public function testDiffForHumansOtherAndFutureMonth() 578 | { 579 | $d = Carbon::now()->subWeeks(4); 580 | $this->assertSame('1 month after', Carbon::now()->diffForHumans($d)); 581 | $d = Carbon::now()->subMonth(); 582 | $this->assertSame('1 month after', Carbon::now()->diffForHumans($d)); 583 | } 584 | public function testDiffForHumansOtherAndFutureMonths() 585 | { 586 | $d = Carbon::now()->subMonths(2); 587 | $this->assertSame('2 months after', Carbon::now()->diffForHumans($d)); 588 | } 589 | public function testDiffForHumansOtherAndNearlyFutureYear() 590 | { 591 | $d = Carbon::now()->subMonths(11); 592 | $this->assertSame('11 months after', Carbon::now()->diffForHumans($d)); 593 | } 594 | public function testDiffForHumansOtherAndFutureYear() 595 | { 596 | $d = Carbon::now()->subYear(); 597 | $this->assertSame('1 year after', Carbon::now()->diffForHumans($d)); 598 | } 599 | public function testDiffForHumansOtherAndFutureYears() 600 | { 601 | $d = Carbon::now()->subYears(2); 602 | $this->assertSame('2 years after', Carbon::now()->diffForHumans($d)); 603 | } 604 | } 605 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | > **This file is autogenerated. Please see the [Contributing](#about-contributing) section from more information.** 2 | 3 | # Carbon 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/nesbot/carbon/v/stable.png)](https://packagist.org/packages/nesbot/carbon) [![Total Downloads](https://poser.pugx.org/nesbot/carbon/downloads.png)](https://packagist.org/packages/nesbot/carbon) [![Build Status](https://secure.travis-ci.org/briannesbitt/Carbon.png)](http://travis-ci.org/briannesbitt/Carbon) 6 | 7 | A simple API extension for DateTime with PHP 5.3+ 8 | 9 | ```php 10 | printf("Right now is %s", Carbon::now()->toDateTimeString()); 11 | printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString() 12 | $tomorrow = Carbon::now()->addDay(); 13 | $lastWeek = Carbon::now()->subWeek(); 14 | $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4); 15 | 16 | $officialDate = Carbon::now()->toRFC2822String(); 17 | 18 | $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age; 19 | 20 | $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London'); 21 | 22 | $worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT'); 23 | 24 | // Don't really want to die so mock now 25 | Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1)); 26 | 27 | // comparisons are always done in UTC 28 | if (Carbon::now()->gte($worldWillEnd)) { 29 | die(); 30 | } 31 | 32 | // Phew! Return to normal behaviour 33 | Carbon::setTestNow(); 34 | 35 | if (Carbon::now()->isWeekend()) { 36 | echo 'Party!'; 37 | } 38 | echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago' 39 | 40 | // ... but also does 'from now', 'after' and 'before' 41 | // rolling up to seconds, minutes, hours, days, months, years 42 | 43 | $daysSinceEpoch = Carbon::createFromTimeStamp(0)->diffInDays(); 44 | ``` 45 | 46 | ## README Contents 47 | 48 | * [Installation](#install) 49 | * [Requirements](#requirements) 50 | * [With composer](#install-composer) 51 | * [Without composer](#install-nocomposer) 52 | * [API](#api) 53 | * [Instantiation](#api-instantiation) 54 | * [Testing Aids](#api-testing) 55 | * [Getters](#api-getters) 56 | * [Setters](#api-setters) 57 | * [Fluent Setters](#api-settersfluent) 58 | * [IsSet](#api-isset) 59 | * [String Formatting and Localization](#api-formatting) 60 | * [Common Formats](#api-commonformats) 61 | * [Comparison](#api-comparison) 62 | * [Addition and Subtraction](#api-addsub) 63 | * [Difference](#api-difference) 64 | * [Difference for Humans](#api-humandiff) 65 | * [Modifiers](#api-modifiers) 66 | * [Constants](#api-constants) 67 | * [About](#about) 68 | * [Contributing](#about-contributing) 69 | * [Author](#about-author) 70 | * [License](#about-license) 71 | * [History](#about-history) 72 | * [Why the name Carbon?](#about-whyname) 73 | 74 | 75 | ## Installation 76 | 77 | 78 | ### Requirements 79 | 80 | - Any flavour of PHP 5.3+ should do 81 | - [optional] PHPUnit to execute the test suite 82 | 83 | 84 | ### With Composer 85 | 86 | The easiest way to install Carbon is via [composer](http://getcomposer.org/). Create the following `composer.json` file and run the `php composer.phar install` command to install it. 87 | 88 | ```json 89 | { 90 | "require": { 91 | "nesbot/Carbon": "*" 92 | } 93 | } 94 | ``` 95 | 96 | ```php 97 | 106 | ### Without Composer 107 | 108 | Why are you not using [composer](http://getcomposer.org/)? Download [Carbon.php](https://github.com/briannesbitt/Carbon/blob/master/Carbon/Carbon.php) from the repo and save the file into your project path somewhere. 109 | 110 | ```php 111 | 120 | ## API 121 | 122 | The Carbon class is [inherited](http://php.net/manual/en/keyword.extends.php) from the PHP [DateTime](http://www.php.net/manual/en/class.datetime.php) class. 123 | 124 | ```php 125 | **Note: I live in Ottawa, Ontario, Canada and if the timezone is not specified in the examples then the default of 'America/Toronto' is to be assumed. Typically Ottawa is -0500 but when daylight savings time is on we are -0400.** 135 | 136 | Special care has been taken to ensure timezones are handled correctly, and where appropriate are based on the underlying DateTime implementation. For example all comparisons are done in UTC or in the timezone of the datetime being used. 137 | 138 | ```php 139 | $dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto'); 140 | $dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver'); 141 | 142 | echo $dtVancouver->diffInHours($dtToronto); // 3 143 | ``` 144 | 145 | Also `is` comparisons are done in the timezone of the provided Carbon instance. For example my current timezone is -13 hours from Tokyo. So `Carbon::now('Asia/Tokyo')->isToday()` would only return false for any time past 1 PM my time. This doesn't make sense since `now()` in tokyo is always today in Tokyo. Thus the comparison to `now()` is done in the same timezone as the current instance. 146 | 147 | 148 | ### Instantiation 149 | 150 | There are several different methods available to create a new instance of Carbon. First there is a constructor. It overrides the [parent constructor](http://www.php.net/manual/en/datetime.construct.php) and you are best to read about the first parameter from the PHP manual and understand the date/time string formats it accepts. You'll hopefully find yourself rarely using the constructor but rather relying on the explicit static methods for improved readability. 151 | 152 | ```php 153 | $carbon = new Carbon(); // equivalent to Carbon::now() 154 | $carbon = new Carbon('first day of January 2008', 'America/Vancouver'); 155 | echo get_class($carbon); // 'Carbon\Carbon' 156 | ``` 157 | 158 | You'll notice above that the timezone (2nd) parameter was passed as a string rather than a `\DateTimeZone` instance. All DateTimeZone parameters have been augmented so you can pass a DateTimeZone instance or a string and the timezone will be created for you. This is again shown in the next example which also introduces the `now()` function. 159 | 160 | ```php 161 | $now = Carbon::now(); 162 | 163 | $nowInLondonTz = Carbon::now(new DateTimeZone('Europe/London')); 164 | 165 | // or just pass the timezone as a string 166 | $nowInLondonTz = Carbon::now('Europe/London'); 167 | ``` 168 | 169 | If you really love your fluid method calls and get frustrated by the extra line or ugly pair of brackets necessary when using the constructor you'll enjoy the `parse` method. 170 | 171 | ```php 172 | echo (new Carbon('first day of December 2008'))->addWeeks(2); // 2008-12-15 00:00:00 173 | echo Carbon::parse('first day of December 2008')->addWeeks(2); // 2008-12-15 00:00:00 174 | ``` 175 | 176 | To accompany `now()`, a few other static instantiation helpers exist to create widely known instances. The only thing to really notice here is that `today()`, `tomorrow()` and `yesterday()`, besides behaving as expected, all accept a timezone parameter and each has their time value set to `00:00:00`. 177 | 178 | ```php 179 | $now = Carbon::now(); 180 | echo $now; // 2013-11-20 18:52:27 181 | $today = Carbon::today(); 182 | echo $today; // 2013-11-20 00:00:00 183 | $tomorrow = Carbon::tomorrow('Europe/London'); 184 | echo $tomorrow; // 2013-11-21 00:00:00 185 | $yesterday = Carbon::yesterday(); 186 | echo $yesterday; // 2013-11-19 00:00:00 187 | ``` 188 | 189 | The next group of static helpers are the `createXXX()` helpers. Most of the static `create` functions allow you to provide as many or as few arguments as you want and will provide default values for all others. Generally default values are the current date, time or timezone. Higher values will wrap appropriately but invalid values will throw an `InvalidArgumentException` with an informative message. The message is obtained from an [DateTime::getLastErrors()](http://php.net/manual/en/datetime.getlasterrors.php) call. 190 | 191 | ```php 192 | Carbon::createFromDate($year, $month, $day, $tz); 193 | Carbon::createFromTime($hour, $minute, $second, $tz); 194 | Carbon::create($year, $month, $day, $hour, $minute, $second, $tz); 195 | ``` 196 | 197 | `createFromDate()` will default the time to now. `createFromTime()` will default the date to today. `create()` will default any null parameter to the current respective value. As before, the `$tz` defaults to the current timezone and otherwise can be a DateTimeZone instance or simply a string timezone value. The only special case for default values (mimicking the underlying PHP library) occurs when an hour value is specified but no minutes or seconds, they will get defaulted to 0. 198 | 199 | ```php 200 | $xmasThisYear = Carbon::createFromDate(null, 12, 25); // Year defaults to current year 201 | $Y2K = Carbon::create(2000, 1, 1, 0, 0, 0); 202 | $alsoY2K = Carbon::create(1999, 12, 31, 24); 203 | $noonLondonTz = Carbon::createFromTime(12, 0, 0, 'Europe/London'); 204 | 205 | // A two digit minute could not be found 206 | try { Carbon::create(1975, 5, 21, 22, -2, 0); } catch(InvalidArgumentException $x) { echo $x->getMessage(); } 207 | ``` 208 | 209 | ```php 210 | Carbon::createFromFormat($format, $time, $tz); 211 | ``` 212 | 213 | `createFromFormat()` is mostly a wrapper for the base php function [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php). The difference being again the `$tz` argument can be a DateTimeZone instance or a string timezone value. Also, if there are errors with the format this function will call the `DateTime::getLastErrors()` method and then throw a `InvalidArgumentException` with the errors as the message. If you look at the source for the `createXX()` functions above, they all make a call to `createFromFormat()`. 214 | 215 | ```php 216 | echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00 217 | ``` 218 | 219 | The final two create functions are for working with [unix timestamps](http://en.wikipedia.org/wiki/Unix_time). The first will create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone. The second, `createFromTimestampUTC()`, is different in that the timezone will remain UTC (GMT). The second acts the same as `Carbon::createFromFormat('@'.$timestamp)` but I have just made it a little more explicit. Negative timestamps are also allowed. 220 | 221 | ```php 222 | echo Carbon::createFromTimeStamp(-1)->toDateTimeString(); // 1969-12-31 18:59:59 223 | echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString(); // 1970-01-01 00:59:59 224 | echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString(); // 1969-12-31 23:59:59 225 | ``` 226 | 227 | You can also create a `copy()` of an existing Carbon instance. As expected the date, time and timezone values are all copied to the new instance. 228 | 229 | ```php 230 | $dt = Carbon::now(); 231 | echo $dt->diffInYears($dt->copy()->addYear()); // 1 232 | 233 | // $dt was unchanged and still holds the value of Carbon:now() 234 | ``` 235 | 236 | Finally, if you find yourself inheriting a `\DateTime` instance from another library, fear not! You can create a `Carbon` instance via a friendly `instance()` function. 237 | 238 | ```php 239 | $dt = new \DateTime('first day of January 2008'); // <== instance from another API 240 | $carbon = Carbon::instance($dt); 241 | echo get_class($carbon); // 'Carbon\Carbon' 242 | echo $carbon->toDateTimeString(); // 2008-01-01 00:00:00 243 | ``` 244 | 245 | 246 | ### Testing Aids 247 | 248 | The testing methods allow you to set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: 249 | - A call to the static now() method, ex. Carbon::now() 250 | - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) 251 | - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') 252 | 253 | ```php 254 | $knownDate = Carbon::create(2001, 5, 21, 12); // create testing date 255 | Carbon::setTestNow($knownDate); // set the mock (of course this could be a real mock object) 256 | echo Carbon::now(); // 2001-05-21 12:00:00 257 | echo new Carbon(); // 2001-05-21 12:00:00 258 | echo Carbon::parse(); // 2001-05-21 12:00:00 259 | echo new Carbon('now'); // 2001-05-21 12:00:00 260 | echo Carbon::parse('now'); // 2001-05-21 12:00:00 261 | var_dump(Carbon::hasTestNow()); // bool(true) 262 | Carbon::setTestNow(); // clear the mock 263 | var_dump(Carbon::hasTestNow()); // bool(false) 264 | echo Carbon::now(); // 2013-11-20 18:52:27 265 | ``` 266 | 267 | A more meaning full example: 268 | 269 | ```php 270 | class SeasonalProduct 271 | { 272 | protected $price; 273 | 274 | public function __construct($price) 275 | { 276 | $this->price = $price; 277 | } 278 | 279 | public function getPrice() { 280 | $multiplier = 1; 281 | if (Carbon::now()->month == 12) { 282 | $multiplier = 2; 283 | } 284 | 285 | return $this->price * $multiplier; 286 | } 287 | } 288 | 289 | $product = new SeasonalProduct(100); 290 | Carbon::setTestNow(Carbon::parse('first day of March 2000')); 291 | echo $product->getPrice(); // 100 292 | Carbon::setTestNow(Carbon::parse('first day of December 2000')); 293 | echo $product->getPrice(); // 200 294 | Carbon::setTestNow(Carbon::parse('first day of May 2000')); 295 | echo $product->getPrice(); // 100 296 | Carbon::setTestNow(); 297 | ``` 298 | 299 | Relative phrases are also mocked according to the given "now" instance. 300 | 301 | ```php 302 | $knownDate = Carbon::create(2001, 5, 21, 12); // create testing date 303 | Carbon::setTestNow($knownDate); // set the mock 304 | echo new Carbon('tomorrow'); // 2001-05-22 00:00:00 305 | echo new Carbon('yesterday'); // 2001-05-20 00:00:00 306 | echo new Carbon('next wednesday'); // 2001-05-23 00:00:00 307 | echo new Carbon('last friday'); // 2001-05-18 00:00:00 308 | echo new Carbon('this thursday'); // 2001-05-24 00:00:00 309 | Carbon::setTestNow(); // always clear it ! 310 | ``` 311 | 312 | The list of words that are considered to be relative modifiers are: 313 | - this 314 | - next 315 | - last 316 | - tomorrow 317 | - yesterday 318 | - + 319 | - - 320 | - first 321 | - last 322 | - ago 323 | 324 | Be aware that similar to the next(), previous() and modify() methods some of these relative modifiers will set the time to 00:00:00. 325 | 326 | 327 | ### Getters 328 | 329 | The getters are implemented via PHP's `__get()` method. This enables you to access the value as if it was a property rather than a function call. 330 | 331 | ```php 332 | $dt = Carbon::create(2012, 9, 5, 23, 26, 11); 333 | 334 | // These getters specifically return integers, ie intval() 335 | var_dump($dt->year); // int(2012) 336 | var_dump($dt->month); // int(9) 337 | var_dump($dt->day); // int(5) 338 | var_dump($dt->hour); // int(23) 339 | var_dump($dt->minute); // int(26) 340 | var_dump($dt->second); // int(11) 341 | var_dump($dt->dayOfWeek); // int(3) 342 | var_dump($dt->dayOfYear); // int(248) 343 | var_dump($dt->weekOfYear); // int(36) 344 | var_dump($dt->daysInMonth); // int(30) 345 | var_dump($dt->timestamp); // int(1346901971) 346 | var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(38) calculated vs now in the same tz 347 | var_dump($dt->quarter); // int(3) 348 | 349 | // Returns an int of seconds difference from UTC (+/- sign included) 350 | var_dump(Carbon::createFromTimestampUTC(0)->offset); // int(0) 351 | var_dump(Carbon::createFromTimestamp(0)->offset); // int(-18000) 352 | 353 | // Returns an int of hours difference from UTC (+/- sign included) 354 | var_dump(Carbon::createFromTimestamp(0)->offsetHours); // int(-5) 355 | 356 | // Indicates if day light savings time is on 357 | var_dump(Carbon::createFromDate(2012, 1, 1)->dst); // bool(false) 358 | var_dump(Carbon::createFromDate(2012, 9, 1)->dst); // bool(true) 359 | 360 | // Indicates if the instance is in the same timezone as the local timzezone 361 | var_dump(Carbon::now()->local); // bool(true) 362 | var_dump(Carbon::now('America/Vancouver')->local); // bool(false) 363 | 364 | // Indicates if the instance is in the UTC timezone 365 | var_dump(Carbon::now()->utc); // bool(false) 366 | var_dump(Carbon::now('Europe/London')->utc); // bool(true) 367 | var_dump(Carbon::createFromTimestampUTC(0)->utc); // bool(true) 368 | 369 | // Gets the DateTimeZone instance 370 | echo get_class(Carbon::now()->timezone); // DateTimeZone 371 | echo get_class(Carbon::now()->tz); // DateTimeZone 372 | 373 | // Gets the DateTimeZone instance name, shortcut for ->timezone->getName() 374 | echo Carbon::now()->timezoneName; // America/Toronto 375 | echo Carbon::now()->tzName; // America/Toronto 376 | ``` 377 | 378 | 379 | ### Setters 380 | 381 | The following setters are implemented via PHP's `__set()` method. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC. 382 | 383 | ```php 384 | $dt = Carbon::now(); 385 | 386 | $dt->year = 1975; 387 | $dt->month = 13; // would force year++ and month = 1 388 | $dt->month = 5; 389 | $dt->day = 21; 390 | $dt->hour = 22; 391 | $dt->minute = 32; 392 | $dt->second = 5; 393 | 394 | $dt->timestamp = 169957925; // This will not change the timezone 395 | 396 | // Set the timezone via DateTimeZone instance or string 397 | $dt->timezone = new DateTimeZone('Europe/London'); 398 | $dt->timezone = 'Europe/London'; 399 | $dt->tz = 'Europe/London'; 400 | ``` 401 | 402 | 403 | ### Fluent Setters 404 | 405 | No arguments are optional for the setters, but there are enough variety in the function definitions that you shouldn't need them anyway. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC. 406 | 407 | ```php 408 | $dt = Carbon::now(); 409 | 410 | $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString(); 411 | $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString(); 412 | $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString(); 413 | 414 | $dt->timestamp(169957925)->timezone('Europe/London'); 415 | 416 | $dt->tz('America/Toronto')->setTimezone('America/Vancouver'); 417 | ``` 418 | 419 | 420 | ### IsSet 421 | 422 | The PHP function `__isset()` is implemented. This was done as some external systems (ex. [Twig](http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties)) validate the existence of a property before using it. This is done using the `isset()` or `empty()` method. You can read more about these on the PHP site: [__isset()](http://www.php.net/manual/en/language.oop5.overloading.php#object.isset), [isset()](http://www.php.net/manual/en/function.isset.php), [empty()](http://www.php.net/manual/en/function.empty.php). 423 | 424 | ```php 425 | var_dump(isset(Carbon::now()->iDoNotExist)); // bool(false) 426 | var_dump(isset(Carbon::now()->hour)); // bool(true) 427 | var_dump(empty(Carbon::now()->iDoNotExist)); // bool(true) 428 | var_dump(empty(Carbon::now()->year)); // bool(false) 429 | ``` 430 | 431 | 432 | ### String Formatting and Localization 433 | 434 | All of the available `toXXXString()` methods rely on the base class method [DateTime::format()](http://php.net/manual/en/datetime.format.php). You'll notice the `__toString()` method is defined which allows a Carbon instance to be printed as a pretty date time string when used in a string context. 435 | 436 | ```php 437 | $dt = Carbon::create(1975, 12, 25, 14, 15, 16); 438 | 439 | var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString() 440 | echo $dt->toDateString(); // 1975-12-25 441 | echo $dt->toFormattedDateString(); // Dec 25, 1975 442 | echo $dt->toTimeString(); // 14:15:16 443 | echo $dt->toDateTimeString(); // 1975-12-25 14:15:16 444 | echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM 445 | 446 | // ... of course format() is still available 447 | echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM 448 | ``` 449 | 450 | You can also set the default __toString() format (which defaults to `Y-m-d H:i:s`). 451 | 452 | ```php 453 | Carbon::setDefaultFormat('jS \o\f F, Y g:i:s a'); 454 | $dt = Carbon::create(1975, 12, 25, 14, 15, 16); 455 | echo $dt; // 25th of December, 1975 2:15:16 pm 456 | ``` 457 | 458 | Unfortunately the base class DateTime does not have any localization support. To begin localization support a `formatLocalized($format)` method has been added. The implementation makes a call to [strftime](http://www.php.net/strftime) using the current instance timestamp. If you first set the current locale with [setlocale()](http://www.php.net/setlocale) then the string returned will be formatted in the correct locale. 459 | 460 | ```php 461 | setlocale(LC_TIME, 'German'); 462 | echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975 463 | setlocale(LC_TIME, ''); 464 | echo $dt->formatLocalized('%A %d %B %Y'); // Thursday 25 December 1975 465 | ``` 466 | 467 | 468 | ## Common Formats 469 | 470 | The following are wrappers for the common formats provided in the [DateTime class](http://www.php.net/manual/en/class.datetime.php). 471 | 472 | ```php 473 | $dt = Carbon::now(); 474 | 475 | echo $dt->toATOMString(); // same as $dt->format(DateTime::ATOM); 476 | echo $dt->toCOOKIEString(); 477 | echo $dt->toISO8601String(); 478 | echo $dt->toRFC822String(); 479 | echo $dt->toRFC850String(); 480 | echo $dt->toRFC1036String(); 481 | echo $dt->toRFC1123String(); 482 | echo $dt->toRFC2822String(); 483 | echo $dt->toRFC3339String(); 484 | echo $dt->toRSSString(); 485 | echo $dt->toW3CString(); 486 | ``` 487 | 488 | 489 | ### Comparison 490 | 491 | Simple comparison is offered up via the following functions. Remember that the comparison is done in the UTC timezone so things aren't always as they seem. 492 | 493 | ```php 494 | echo Carbon::now()->tzName; // America/Toronto 495 | $first = Carbon::create(2012, 9, 5, 23, 26, 11); 496 | $second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver'); 497 | 498 | echo $first->toDateTimeString(); // 2012-09-05 23:26:11 499 | echo $first->tzName; // America/Toronto 500 | echo $second->toDateTimeString(); // 2012-09-05 20:26:11 501 | echo $second->tzName; // America/Vancouver 502 | 503 | var_dump($first->eq($second)); // bool(true) 504 | var_dump($first->ne($second)); // bool(false) 505 | var_dump($first->gt($second)); // bool(false) 506 | var_dump($first->gte($second)); // bool(true) 507 | var_dump($first->lt($second)); // bool(false) 508 | var_dump($first->lte($second)); // bool(true) 509 | 510 | $first->setDateTime(2012, 1, 1, 0, 0, 0); 511 | $second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver' 512 | 513 | var_dump($first->eq($second)); // bool(false) 514 | var_dump($first->ne($second)); // bool(true) 515 | var_dump($first->gt($second)); // bool(false) 516 | var_dump($first->gte($second)); // bool(false) 517 | var_dump($first->lt($second)); // bool(true) 518 | var_dump($first->lte($second)); // bool(true) 519 | ``` 520 | 521 | To determine if the current instance is between two other instances you can use the aptly named `between()` method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries. 522 | 523 | ```php 524 | $first = Carbon::create(2012, 9, 5, 1); 525 | $second = Carbon::create(2012, 9, 5, 5); 526 | var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true) 527 | var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true) 528 | var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false) 529 | ``` 530 | 531 | To handle the most used cases there are some simple helper functions that hopefully are obvious from their names. For the methods that compare to `now()` (ex. isToday()) in some manner the `now()` is created in the same timezone as the instance. 532 | 533 | ```php 534 | $dt = Carbon::now(); 535 | 536 | $dt->isWeekday(); 537 | $dt->isWeekend(); 538 | $dt->isYesterday(); 539 | $dt->isToday(); 540 | $dt->isTomorrow(); 541 | $dt->isFuture(); 542 | $dt->isPast(); 543 | $dt->isLeapYear(); 544 | ``` 545 | 546 | 547 | ### Addition and Subtraction 548 | 549 | The default DateTime provides a couple of different methods for easily adding and subtracting time. There is `modify()`, `add()` and `sub()`. `modify()` takes a *magical* date/time format string, 'last day of next month', that it parses and applies the modification while `add()` and `sub()` use a `DateInterval` class thats not so obvious, `new \DateInterval('P6YT5M')`. Hopefully using these fluent functions will be more clear and easier to read after not seeing your code for a few weeks. But of course I don't make you choose since the base class functions are still available. 550 | 551 | ```php 552 | $dt = Carbon::create(2012, 1, 31, 0); 553 | 554 | echo $dt->toDateTimeString(); // 2012-01-31 00:00:00 555 | 556 | echo $dt->addYears(5); // 31st of January, 2017 12:00:00 am 557 | echo $dt->addYear(); // 31st of January, 2018 12:00:00 am 558 | echo $dt->subYear(); // 31st of January, 2017 12:00:00 am 559 | echo $dt->subYears(5); // 31st of January, 2012 12:00:00 am 560 | 561 | echo $dt->addMonths(60); // 31st of January, 2017 12:00:00 am 562 | echo $dt->addMonth(); // 3rd of March, 2017 12:00:00 am equivalent of $dt->month($dt->month + 1); so it wraps 563 | echo $dt->subMonth(); // 3rd of February, 2017 12:00:00 am 564 | echo $dt->subMonths(60); // 3rd of February, 2012 12:00:00 am 565 | 566 | echo $dt->addDays(29); // 3rd of March, 2012 12:00:00 am 567 | echo $dt->addDay(); // 4th of March, 2012 12:00:00 am 568 | echo $dt->subDay(); // 3rd of March, 2012 12:00:00 am 569 | echo $dt->subDays(29); // 3rd of February, 2012 12:00:00 am 570 | 571 | echo $dt->addWeekdays(4); // 9th of February, 2012 12:00:00 am 572 | echo $dt->addWeekday(); // 10th of February, 2012 12:00:00 am 573 | echo $dt->subWeekday(); // 9th of February, 2012 12:00:00 am 574 | echo $dt->subWeekdays(4); // 3rd of February, 2012 12:00:00 am 575 | 576 | echo $dt->addWeeks(3); // 24th of February, 2012 12:00:00 am 577 | echo $dt->addWeek(); // 2nd of March, 2012 12:00:00 am 578 | echo $dt->subWeek(); // 24th of February, 2012 12:00:00 am 579 | echo $dt->subWeeks(3); // 3rd of February, 2012 12:00:00 am 580 | 581 | echo $dt->addHours(24); // 4th of February, 2012 12:00:00 am 582 | echo $dt->addHour(); // 4th of February, 2012 1:00:00 am 583 | echo $dt->subHour(); // 4th of February, 2012 12:00:00 am 584 | echo $dt->subHours(24); // 3rd of February, 2012 12:00:00 am 585 | 586 | echo $dt->addMinutes(61); // 3rd of February, 2012 1:01:00 am 587 | echo $dt->addMinute(); // 3rd of February, 2012 1:02:00 am 588 | echo $dt->subMinute(); // 3rd of February, 2012 1:01:00 am 589 | echo $dt->subMinutes(61); // 3rd of February, 2012 12:00:00 am 590 | 591 | echo $dt->addSeconds(61); // 3rd of February, 2012 12:01:01 am 592 | echo $dt->addSecond(); // 3rd of February, 2012 12:01:02 am 593 | echo $dt->subSecond(); // 3rd of February, 2012 12:01:01 am 594 | echo $dt->subSeconds(61); // 3rd of February, 2012 12:00:00 am 595 | ``` 596 | 597 | For fun you can also pass negative values to `addXXX()`, in fact that's how `subXXX()` is implemented. 598 | 599 | 600 | ### Difference 601 | 602 | These functions always return the **total difference** expressed in the specified time requested. This differs from the base class `diff()` function where an interval of 61 seconds would be returned as 1 minute and 1 second via a `DateInterval` instance. The `diffInMinutes()` function would simply return 1. All values are truncated and not rounded. Each function below has a default first parameter which is the Carbon instance to compare to, or null if you want to use `now()`. The 2nd parameter again is optional and indicates if you want the return value to be the absolute value or a relative value that might have a `-` (negative) sign if the passed in date is less than the current instance. This will default to true, return the absolute value. The comparisons are done in UTC. 603 | 604 | ```php 605 | // Carbon::diffInYears(Carbon $dt = null, $abs = true) 606 | 607 | echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0 608 | 609 | $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 610 | $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'); 611 | echo $dtOttawa->diffInHours($dtVancouver); // 3 612 | 613 | echo $dtOttawa->diffInHours($dtVancouver, false); // 3 614 | echo $dtVancouver->diffInHours($dtOttawa, false); // -3 615 | 616 | $dt = Carbon::create(2012, 1, 31, 0); 617 | echo $dt->diffInDays($dt->copy()->addMonth()); // 31 618 | echo $dt->diffInDays($dt->copy()->subMonth(), false); // -31 619 | 620 | $dt = Carbon::create(2012, 4, 30, 0); 621 | echo $dt->diffInDays($dt->copy()->addMonth()); // 30 622 | echo $dt->diffInDays($dt->copy()->addWeek()); // 7 623 | 624 | $dt = Carbon::create(2012, 1, 1, 0); 625 | echo $dt->diffInMinutes($dt->copy()->addSeconds(59)); // 0 626 | echo $dt->diffInMinutes($dt->copy()->addSeconds(60)); // 1 627 | echo $dt->diffInMinutes($dt->copy()->addSeconds(119)); // 1 628 | echo $dt->diffInMinutes($dt->copy()->addSeconds(120)); // 2 629 | 630 | // others that are defined 631 | // diffInYears(), diffInMonths(), diffInDays() 632 | // diffInHours(), diffInMinutes(), diffInSeconds() 633 | ``` 634 | 635 | 636 | ### Difference for Humans 637 | 638 | It is easier for humans to read `1 month ago` compared to 30 days ago. This is a common function seen in most date libraries so I thought I would add it here as well. It uses approximations for a month being 4 weeks. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to `now()` if not specified. 639 | 640 | This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities: 641 | 642 | * When comparing a value in the past to default now: 643 | * 1 hour ago 644 | * 5 months ago 645 | 646 | * When comparing a value in the future to default now: 647 | * 1 hour from now 648 | * 5 months from now 649 | 650 | * When comparing a value in the past to another value: 651 | * 1 hour before 652 | * 5 months before 653 | 654 | * When comparing a value in the future to another value: 655 | * 1 hour after 656 | * 5 months after 657 | 658 | ```php 659 | // The most typical usage is for comments 660 | // The instance is the date the comment was created and its being compared to default now() 661 | echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago 662 | 663 | echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after 664 | 665 | $dt = Carbon::createFromDate(2011, 2, 1); 666 | 667 | echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before 668 | echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after 669 | 670 | echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 seconds from now 671 | 672 | echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago 673 | ``` 674 | 675 | 676 | ### Modifiers 677 | 678 | These group of methods perform helpful modifications to the current instance. Most of them are self explanatory from their names... or at least should be. You'll also notice that the startOfXXX(), next() and previous() methods set the time to 00:00:00 and the endOfXXX() methods set the time to 23:59:59. 679 | 680 | ```php 681 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 682 | echo $dt->startOfDay(); // 31st of January, 2012 12:00:00 am 683 | 684 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 685 | echo $dt->endOfDay(); // 31st of January, 2012 11:59:59 pm 686 | 687 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 688 | echo $dt->startOfMonth(); // 1st of January, 2012 12:00:00 am 689 | 690 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 691 | echo $dt->endOfMonth(); // 31st of January, 2012 11:59:59 pm 692 | 693 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 694 | echo $dt->startOfWeek(); // 30th of January, 2012 12:00:00 am 695 | var_dump($dt->dayOfWeek == Carbon::MONDAY); // bool(true) : ISO8601 week starts on Monday 696 | 697 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 698 | echo $dt->endOfWeek(); // 5th of February, 2012 11:59:59 pm 699 | var_dump($dt->dayOfWeek == Carbon::SUNDAY); // bool(true) : ISO8601 week ends on Sunday 700 | 701 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 702 | echo $dt->next(Carbon::WEDNESDAY); // 1st of February, 2012 12:00:00 am 703 | var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true) 704 | 705 | $dt = Carbon::create(2012, 1, 1, 12, 0, 0); 706 | echo $dt->next(); // 8th of January, 2012 12:00:00 am 707 | 708 | $dt = Carbon::create(2012, 1, 31, 12, 0, 0); 709 | echo $dt->previous(Carbon::WEDNESDAY); // 25th of January, 2012 12:00:00 am 710 | var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true) 711 | 712 | $dt = Carbon::create(2012, 1, 1, 12, 0, 0); 713 | echo $dt->previous(); // 25th of December, 2011 12:00:00 am 714 | 715 | // others that are defined that are similar 716 | // firstOfMonth(), lastOfMonth(), nthOfMonth() 717 | // firstOfQuarter(), lastOfQuarter(), nthOfQuarter() 718 | // firstOfYear(), lastOfYear(), nthOfYear() 719 | ``` 720 | 721 | 722 | ### Constants 723 | 724 | The following constants are defined in the Carbon class. 725 | 726 | * SUNDAY = 0 727 | * MONDAY = 1 728 | * TUESDAY = 2 729 | * WEDNESDAY = 3 730 | * THURSDAY = 4 731 | * FRIDAY = 5 732 | * SATURDAY = 6 733 | * MONTHS_PER_YEAR = 12 734 | * HOURS_PER_DAY = 24 735 | * MINUTES_PER_HOUR = 60 736 | * SECONDS_PER_MINUTE = 60 737 | 738 | ```php 739 | $dt = Carbon::createFromDate(2012, 10, 6); 740 | if ($dt->dayOfWeek === Carbon::SATURDAY) { 741 | echo 'Place bets on Ottawa Senators Winning!'; 742 | } 743 | ``` 744 | 745 | 746 | ## About 747 | 748 | 749 | ### Contributing 750 | 751 | I hate reading a readme.md file that has code errors and/or sample output that is incorrect. I tried something new with this project and wrote a quick readme parser that can **lint** sample source code or **execute** and inject the actual result into a generated readme. 752 | 753 | > **Don't make changes to the `readme.md` directly!!** 754 | 755 | Change the `readme.src.md` and then use the `readme.php` to generate the new `readme.md` file. It can be run at the command line using `php readme.php` from the project root. Maybe someday I'll extract this out to another project or at least run it with a post receive hook, but for now its just a local tool, deal with it. 756 | 757 | The commands are quickly explained below. To see some examples you can view the raw `readme.src.md` file in this repo. 758 | 759 | `{{::lint()}}` 760 | 761 | The `lint` command is meant for confirming the code is valid and will `eval()` the code passed into the function. Assuming there were no errors, the executed source code will then be injected back into the text replacing out the `{{::lint()}}`. When you look at the raw `readme.src.md` you will see that the code can span several lines. Remember the code is executed in the context of the running script so any variables will be available for the rest of the file. 762 | 763 | {{::lint($var = 'brian nesbitt';)}} => $var = 'brian nesbitt'; 764 | 765 | > As mentioned the `$var` can later be echo'd and you would get 'brian nesbitt' as all of the source is executed in the same scope. 766 | 767 | `{{varName::exec()}}` and `{{varName_eval}}` 768 | 769 | The `exec` command begins by performing an `eval()` on the code passed into the function. The executed source code will then be injected back into the text replacing out the `{{varName::exec()}}`. This will also create a variable named `varName_eval` that you can then place anywhere in the file and it will get replaced with the output of the `eval()`. You can use any type of output (`echo`, `printf`, `var_dump` etc) statement to return the result value as an output buffer is setup to capture the output. 770 | 771 | {{exVarName::exec(echo $var;)}} => echo $var; 772 | {{exVarName_eval}} => brian nesbitt // $var is still set from above 773 | 774 | `/*pad()*/` 775 | 776 | The `pad()` is a special source modifier. This will pad the code block to the indicated number of characters using spaces. Its particularly handy for aligning `//` comments when showing results. 777 | 778 | {{exVarName1::exec(echo 12345;/*pad(20)*/)}} // {{exVarName1_eval}} 779 | {{exVarName2::exec(echo 6;/*pad(20)*/)}} // {{exVarName2_eval}} 780 | 781 | ... would generate to: 782 | 783 | echo 12345; // 12345 784 | echo 6; // 6 785 | 786 | Apart from the readme the typical steps can be used to contribute your own improvements. 787 | 788 | * Fork 789 | * Clone 790 | * PHPUnit 791 | * Branch 792 | * PHPUnit 793 | * Code 794 | * PHPUnit 795 | * Commit 796 | * Push 797 | * Pull request 798 | * Relax and play Castle Crashers 799 | 800 | 801 | ### Author 802 | 803 | Brian Nesbitt - - 804 | 805 | 806 | ### License 807 | 808 | Carbon is licensed under the MIT License - see the `LICENSE` file for details 809 | 810 | 811 | ### History 812 | 813 | You can view the history of the Carbon project in the [history file](https://github.com/briannesbitt/Carbon/blob/master/history.md). 814 | 815 | 816 | ### Why the name Carbon? 817 | 818 | Read about [Carbon Dating](http://en.wikipedia.org/wiki/Radiocarbon_dating) -------------------------------------------------------------------------------- /readme.src.md: -------------------------------------------------------------------------------- 1 | > **This file is autogenerated. Please see the [Contributing](#about-contributing) section from more information.** 2 | 3 | # Carbon 4 | 5 | [![Latest Stable Version](https://poser.pugx.org/nesbot/carbon/v/stable.png)](https://packagist.org/packages/nesbot/carbon) [![Total Downloads](https://poser.pugx.org/nesbot/carbon/downloads.png)](https://packagist.org/packages/nesbot/carbon) [![Build Status](https://secure.travis-ci.org/briannesbitt/Carbon.png)](http://travis-ci.org/briannesbitt/Carbon) 6 | 7 | A simple API extension for DateTime with PHP 5.3+ 8 | 9 | ```php 10 | {{::lint( 11 | printf("Right now is %s", Carbon::now()->toDateTimeString()); 12 | printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString() 13 | $tomorrow = Carbon::now()->addDay(); 14 | $lastWeek = Carbon::now()->subWeek(); 15 | $nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4); 16 | 17 | $officialDate = Carbon::now()->toRFC2822String(); 18 | 19 | $howOldAmI = Carbon::createFromDate(1975, 5, 21)->age; 20 | 21 | $noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London'); 22 | 23 | $worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT'); 24 | 25 | // Don't really want to die so mock now 26 | Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1)); 27 | 28 | // comparisons are always done in UTC 29 | if (Carbon::now()->gte($worldWillEnd)) { 30 | die(); 31 | } 32 | 33 | // Phew! Return to normal behaviour 34 | Carbon::setTestNow(); 35 | 36 | if (Carbon::now()->isWeekend()) { 37 | echo 'Party!'; 38 | } 39 | )}} 40 | {{intro::exec(echo Carbon::now()->subMinutes(2)->diffForHumans();)}} // '{{intro_eval}}' 41 | 42 | // ... but also does 'from now', 'after' and 'before' 43 | // rolling up to seconds, minutes, hours, days, months, years 44 | 45 | {{::lint( 46 | $daysSinceEpoch = Carbon::createFromTimeStamp(0)->diffInDays(); 47 | )}} 48 | ``` 49 | 50 | ## README Contents 51 | 52 | * [Installation](#install) 53 | * [Requirements](#requirements) 54 | * [With composer](#install-composer) 55 | * [Without composer](#install-nocomposer) 56 | * [API](#api) 57 | * [Instantiation](#api-instantiation) 58 | * [Testing Aids](#api-testing) 59 | * [Getters](#api-getters) 60 | * [Setters](#api-setters) 61 | * [Fluent Setters](#api-settersfluent) 62 | * [IsSet](#api-isset) 63 | * [String Formatting and Localization](#api-formatting) 64 | * [Common Formats](#api-commonformats) 65 | * [Comparison](#api-comparison) 66 | * [Addition and Subtraction](#api-addsub) 67 | * [Difference](#api-difference) 68 | * [Difference for Humans](#api-humandiff) 69 | * [Modifiers](#api-modifiers) 70 | * [Constants](#api-constants) 71 | * [About](#about) 72 | * [Contributing](#about-contributing) 73 | * [Author](#about-author) 74 | * [License](#about-license) 75 | * [History](#about-history) 76 | * [Why the name Carbon?](#about-whyname) 77 | 78 | 79 | ## Installation 80 | 81 | 82 | ### Requirements 83 | 84 | - Any flavour of PHP 5.3+ should do 85 | - [optional] PHPUnit to execute the test suite 86 | 87 | 88 | ### With Composer 89 | 90 | The easiest way to install Carbon is via [composer](http://getcomposer.org/). Create the following `composer.json` file and run the `php composer.phar install` command to install it. 91 | 92 | ```json 93 | { 94 | "require": { 95 | "nesbot/Carbon": "*" 96 | } 97 | } 98 | ``` 99 | 100 | ```php 101 | 110 | ### Without Composer 111 | 112 | Why are you not using [composer](http://getcomposer.org/)? Download [Carbon.php](https://github.com/briannesbitt/Carbon/blob/master/Carbon/Carbon.php) from the repo and save the file into your project path somewhere. 113 | 114 | ```php 115 | 124 | ## API 125 | 126 | The Carbon class is [inherited](http://php.net/manual/en/keyword.extends.php) from the PHP [DateTime](http://www.php.net/manual/en/class.datetime.php) class. 127 | 128 | ```php 129 | **Note: I live in Ottawa, Ontario, Canada and if the timezone is not specified in the examples then the default of 'America/Toronto' is to be assumed. Typically Ottawa is -0500 but when daylight savings time is on we are -0400.** 139 | 140 | Special care has been taken to ensure timezones are handled correctly, and where appropriate are based on the underlying DateTime implementation. For example all comparisons are done in UTC or in the timezone of the datetime being used. 141 | 142 | ```php 143 | {{::lint($dtToronto = Carbon::createFromDate(2012, 1, 1, 'America/Toronto');)}} 144 | {{::lint($dtVancouver = Carbon::createFromDate(2012, 1, 1, 'America/Vancouver');)}} 145 | 146 | {{tz::exec(echo $dtVancouver->diffInHours($dtToronto);)}} // {{tz_eval}} 147 | ``` 148 | 149 | Also `is` comparisons are done in the timezone of the provided Carbon instance. For example my current timezone is -13 hours from Tokyo. So `Carbon::now('Asia/Tokyo')->isToday()` would only return false for any time past 1 PM my time. This doesn't make sense since `now()` in tokyo is always today in Tokyo. Thus the comparison to `now()` is done in the same timezone as the current instance. 150 | 151 | 152 | ### Instantiation 153 | 154 | There are several different methods available to create a new instance of Carbon. First there is a constructor. It overrides the [parent constructor](http://www.php.net/manual/en/datetime.construct.php) and you are best to read about the first parameter from the PHP manual and understand the date/time string formats it accepts. You'll hopefully find yourself rarely using the constructor but rather relying on the explicit static methods for improved readability. 155 | 156 | ```php 157 | {{::lint($carbon = new Carbon();/*pad(40)*/)}} // equivalent to Carbon::now() 158 | {{::lint($carbon = new Carbon('first day of January 2008', 'America/Vancouver');)}} 159 | {{ctorType::exec(echo get_class($carbon);/*pad(40)*/)}} // '{{ctorType_eval}}' 160 | ``` 161 | 162 | You'll notice above that the timezone (2nd) parameter was passed as a string rather than a `\DateTimeZone` instance. All DateTimeZone parameters have been augmented so you can pass a DateTimeZone instance or a string and the timezone will be created for you. This is again shown in the next example which also introduces the `now()` function. 163 | 164 | ```php 165 | {{::lint( 166 | $now = Carbon::now(); 167 | 168 | $nowInLondonTz = Carbon::now(new DateTimeZone('Europe/London')); 169 | 170 | // or just pass the timezone as a string 171 | $nowInLondonTz = Carbon::now('Europe/London'); 172 | )}} 173 | ``` 174 | 175 | If you really love your fluid method calls and get frustrated by the extra line or ugly pair of brackets necessary when using the constructor you'll enjoy the `parse` method. 176 | 177 | ```php 178 | {{parse1::exec(echo (new Carbon('first day of December 2008'))->addWeeks(2);/*pad(65)*/)}} // {{parse1_eval}} 179 | {{parse2::exec(echo Carbon::parse('first day of December 2008')->addWeeks(2);/*pad(65)*/)}} // {{parse2_eval}} 180 | ``` 181 | 182 | To accompany `now()`, a few other static instantiation helpers exist to create widely known instances. The only thing to really notice here is that `today()`, `tomorrow()` and `yesterday()`, besides behaving as expected, all accept a timezone parameter and each has their time value set to `00:00:00`. 183 | 184 | ```php 185 | {{::lint($now = Carbon::now();)}} 186 | {{now::exec(echo $now;/*pad(40)*/)}} // {{now_eval}} 187 | {{::lint($today = Carbon::today();)}} 188 | {{today::exec(echo $today;/*pad(40)*/)}} // {{today_eval}} 189 | {{::lint($tomorrow = Carbon::tomorrow('Europe/London');)}} 190 | {{tomorrow::exec(echo $tomorrow;/*pad(40)*/)}} // {{tomorrow_eval}} 191 | {{::lint($yesterday = Carbon::yesterday();)}} 192 | {{yesterday::exec(echo $yesterday;/*pad(40)*/)}} // {{yesterday_eval}} 193 | ``` 194 | 195 | The next group of static helpers are the `createXXX()` helpers. Most of the static `create` functions allow you to provide as many or as few arguments as you want and will provide default values for all others. Generally default values are the current date, time or timezone. Higher values will wrap appropriately but invalid values will throw an `InvalidArgumentException` with an informative message. The message is obtained from an [DateTime::getLastErrors()](http://php.net/manual/en/datetime.getlasterrors.php) call. 196 | 197 | ```php 198 | Carbon::createFromDate($year, $month, $day, $tz); 199 | Carbon::createFromTime($hour, $minute, $second, $tz); 200 | Carbon::create($year, $month, $day, $hour, $minute, $second, $tz); 201 | ``` 202 | 203 | `createFromDate()` will default the time to now. `createFromTime()` will default the date to today. `create()` will default any null parameter to the current respective value. As before, the `$tz` defaults to the current timezone and otherwise can be a DateTimeZone instance or simply a string timezone value. The only special case for default values (mimicking the underlying PHP library) occurs when an hour value is specified but no minutes or seconds, they will get defaulted to 0. 204 | 205 | ```php 206 | {{::lint( 207 | $xmasThisYear = Carbon::createFromDate(null, 12, 25); // Year defaults to current year 208 | $Y2K = Carbon::create(2000, 1, 1, 0, 0, 0); 209 | $alsoY2K = Carbon::create(1999, 12, 31, 24); 210 | $noonLondonTz = Carbon::createFromTime(12, 0, 0, 'Europe/London'); 211 | )}} 212 | 213 | // {{createFromDateException_eval}} 214 | {{createFromDateException::exec(try { Carbon::create(1975, 5, 21, 22, -2, 0); } catch(InvalidArgumentException $x) { echo $x->getMessage(); })}} 215 | ``` 216 | 217 | ```php 218 | Carbon::createFromFormat($format, $time, $tz); 219 | ``` 220 | 221 | `createFromFormat()` is mostly a wrapper for the base php function [DateTime::createFromFormat](http://php.net/manual/en/datetime.createfromformat.php). The difference being again the `$tz` argument can be a DateTimeZone instance or a string timezone value. Also, if there are errors with the format this function will call the `DateTime::getLastErrors()` method and then throw a `InvalidArgumentException` with the errors as the message. If you look at the source for the `createXX()` functions above, they all make a call to `createFromFormat()`. 222 | 223 | ```php 224 | {{createFromFormat1::exec(echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString();)}} // {{createFromFormat1_eval}} 225 | ``` 226 | 227 | The final two create functions are for working with [unix timestamps](http://en.wikipedia.org/wiki/Unix_time). The first will create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone. The second, `createFromTimestampUTC()`, is different in that the timezone will remain UTC (GMT). The second acts the same as `Carbon::createFromFormat('@'.$timestamp)` but I have just made it a little more explicit. Negative timestamps are also allowed. 228 | 229 | ```php 230 | {{createFromTimeStamp1::exec(echo Carbon::createFromTimeStamp(-1)->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStamp1_eval}} 231 | {{createFromTimeStamp2::exec(echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStamp2_eval}} 232 | {{createFromTimeStampUTC::exec(echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString();/*pad(80)*/)}} // {{createFromTimeStampUTC_eval}} 233 | ``` 234 | 235 | You can also create a `copy()` of an existing Carbon instance. As expected the date, time and timezone values are all copied to the new instance. 236 | 237 | ```php 238 | {{::lint($dt = Carbon::now();)}} 239 | {{copy2::exec(echo $dt->diffInYears($dt->copy()->addYear());)}} // {{copy2_eval}} 240 | 241 | // $dt was unchanged and still holds the value of Carbon:now() 242 | ``` 243 | 244 | Finally, if you find yourself inheriting a `\DateTime` instance from another library, fear not! You can create a `Carbon` instance via a friendly `instance()` function. 245 | 246 | ```php 247 | {{::lint($dt = new \DateTime('first day of January 2008');)}} // <== instance from another API 248 | {{::lint($carbon = Carbon::instance($dt);)}} 249 | {{ctorType1::exec(echo get_class($carbon);/*pad(54)*/)}} // '{{ctorType1_eval}}' 250 | {{ctorType2::exec(echo $carbon->toDateTimeString();/*pad(54)*/)}} // {{ctorType2_eval}} 251 | ``` 252 | 253 | 254 | ### Testing Aids 255 | 256 | The testing methods allow you to set a Carbon instance (real or mock) to be returned when a "now" instance is created. The provided instance will be returned specifically under the following conditions: 257 | - A call to the static now() method, ex. Carbon::now() 258 | - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null) 259 | - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now') 260 | 261 | ```php 262 | {{::lint($knownDate = Carbon::create(2001, 5, 21, 12);/*pad(54)*/)}} // create testing date 263 | {{::lint(Carbon::setTestNow($knownDate);/*pad(54)*/)}} // set the mock (of course this could be a real mock object) 264 | {{testaid1::exec(echo Carbon::now();/*pad(54)*/)}} // {{testaid1_eval}} 265 | {{testaid2::exec(echo new Carbon();/*pad(54)*/)}} // {{testaid2_eval}} 266 | {{testaid3::exec(echo Carbon::parse();/*pad(54)*/)}} // {{testaid3_eval}} 267 | {{testaid4::exec(echo new Carbon('now');/*pad(54)*/)}} // {{testaid4_eval}} 268 | {{testaid5::exec(echo Carbon::parse('now');/*pad(54)*/)}} // {{testaid5_eval}} 269 | {{hasTestNow::exec(var_dump(Carbon::hasTestNow());/*pad(54)*/)}} // {{hasTestNow_eval}} 270 | {{::lint(Carbon::setTestNow();/*pad(54)*/)}} // clear the mock 271 | {{hasTestNowNo::exec(var_dump(Carbon::hasTestNow());/*pad(54)*/)}} // {{hasTestNowNo_eval}} 272 | {{backToNormal::exec(echo Carbon::now();/*pad(54)*/)}} // {{backToNormal_eval}} 273 | ``` 274 | 275 | A more meaning full example: 276 | 277 | ```php 278 | {{::lint( 279 | class SeasonalProduct 280 | { 281 | protected $price; 282 | 283 | public function __construct($price) 284 | { 285 | $this->price = $price; 286 | } 287 | 288 | public function getPrice() { 289 | $multiplier = 1; 290 | if (Carbon::now()->month == 12) { 291 | $multiplier = 2; 292 | } 293 | 294 | return $this->price * $multiplier; 295 | } 296 | } 297 | 298 | $product = new SeasonalProduct(100); 299 | )}} 300 | {{::lint(Carbon::setTestNow(Carbon::parse('first day of March 2000'));/*pad(40)*/)}} 301 | {{product1::exec(echo $product->getPrice();/*pad(70)*/)}} // {{product1_eval}} 302 | {{::lint(Carbon::setTestNow(Carbon::parse('first day of December 2000'));/*pad(40)*/)}} 303 | {{product2::exec(echo $product->getPrice();/*pad(70)*/)}} // {{product2_eval}} 304 | {{::lint(Carbon::setTestNow(Carbon::parse('first day of May 2000'));/*pad(40)*/)}} 305 | {{product3::exec(echo $product->getPrice();/*pad(70)*/)}} // {{product3_eval}} 306 | {{::lint(Carbon::setTestNow();)}} 307 | ``` 308 | 309 | Relative phrases are also mocked according to the given "now" instance. 310 | 311 | ```php 312 | {{::lint($knownDate = Carbon::create(2001, 5, 21, 12);/*pad(54)*/)}} // create testing date 313 | {{::lint(Carbon::setTestNow($knownDate);/*pad(54)*/)}} // set the mock 314 | {{testaid6::exec(echo new Carbon('tomorrow');/*pad(54)*/)}} // {{testaid6_eval}} 315 | {{testaid7::exec(echo new Carbon('yesterday');/*pad(54)*/)}} // {{testaid7_eval}} 316 | {{testaid8::exec(echo new Carbon('next wednesday');/*pad(54)*/)}} // {{testaid8_eval}} 317 | {{testaid9::exec(echo new Carbon('last friday');/*pad(54)*/)}} // {{testaid9_eval}} 318 | {{testaid10::exec(echo new Carbon('this thursday');/*pad(54)*/)}} // {{testaid10_eval}} 319 | {{::exec(Carbon::setTestNow();/*pad(54)*/)}} // always clear it ! 320 | ``` 321 | 322 | The list of words that are considered to be relative modifiers are: 323 | - this 324 | - next 325 | - last 326 | - tomorrow 327 | - yesterday 328 | - + 329 | - - 330 | - first 331 | - last 332 | - ago 333 | 334 | Be aware that similar to the next(), previous() and modify() methods some of these relative modifiers will set the time to 00:00:00. 335 | 336 | 337 | ### Getters 338 | 339 | The getters are implemented via PHP's `__get()` method. This enables you to access the value as if it was a property rather than a function call. 340 | 341 | ```php 342 | {{::lint($dt = Carbon::create(2012, 9, 5, 23, 26, 11);)}} 343 | 344 | // These getters specifically return integers, ie intval() 345 | {{getyear::exec(var_dump($dt->year);/*pad(60)*/)}} // {{getyear_eval}} 346 | {{getmonth::exec(var_dump($dt->month);/*pad(60)*/)}} // {{getmonth_eval}} 347 | {{getday::exec(var_dump($dt->day);/*pad(60)*/)}} // {{getday_eval}} 348 | {{gethour::exec(var_dump($dt->hour);/*pad(60)*/)}} // {{gethour_eval}} 349 | {{getminute::exec(var_dump($dt->minute);/*pad(60)*/)}} // {{getminute_eval}} 350 | {{getsecond::exec(var_dump($dt->second);/*pad(60)*/)}} // {{getsecond_eval}} 351 | {{getdow::exec(var_dump($dt->dayOfWeek);/*pad(60)*/)}} // {{getdow_eval}} 352 | {{getdoy::exec(var_dump($dt->dayOfYear);/*pad(60)*/)}} // {{getdoy_eval}} 353 | {{getwoy::exec(var_dump($dt->weekOfYear);/*pad(60)*/)}} // {{getwoy_eval}} 354 | {{getdnm::exec(var_dump($dt->daysInMonth);/*pad(60)*/)}} // {{getdnm_eval}} 355 | {{getts::exec(var_dump($dt->timestamp);/*pad(60)*/)}} // {{getts_eval}} 356 | {{getage::exec(var_dump(Carbon::createFromDate(1975, 5, 21)->age);/*pad(60)*/)}} // {{getage_eval}} calculated vs now in the same tz 357 | {{getq::exec(var_dump($dt->quarter);/*pad(60)*/)}} // {{getq_eval}} 358 | 359 | // Returns an int of seconds difference from UTC (+/- sign included) 360 | {{get1::exec(var_dump(Carbon::createFromTimestampUTC(0)->offset);/*pad(60)*/)}} // {{get1_eval}} 361 | {{get2::exec(var_dump(Carbon::createFromTimestamp(0)->offset);/*pad(60)*/)}} // {{get2_eval}} 362 | 363 | // Returns an int of hours difference from UTC (+/- sign included) 364 | {{get3::exec(var_dump(Carbon::createFromTimestamp(0)->offsetHours);/*pad(60)*/)}} // {{get3_eval}} 365 | 366 | // Indicates if day light savings time is on 367 | {{getdst::exec(var_dump(Carbon::createFromDate(2012, 1, 1)->dst);/*pad(60)*/)}} // {{getdst_eval}} 368 | {{getdst2::exec(var_dump(Carbon::createFromDate(2012, 9, 1)->dst);/*pad(60)*/)}} // {{getdst2_eval}} 369 | 370 | // Indicates if the instance is in the same timezone as the local timzezone 371 | {{getLocal::exec(var_dump(Carbon::now()->local);/*pad(60)*/)}} // {{getLocal_eval}} 372 | {{getLocal2::exec(var_dump(Carbon::now('America/Vancouver')->local);/*pad(60)*/)}} // {{getLocal2_eval}} 373 | 374 | // Indicates if the instance is in the UTC timezone 375 | {{getUTC::exec(var_dump(Carbon::now()->utc);/*pad(60)*/)}} // {{getUTC_eval}} 376 | {{getUTC2::exec(var_dump(Carbon::now('Europe/London')->utc);/*pad(60)*/)}} // {{getUTC2_eval}} 377 | {{getUTC3::exec(var_dump(Carbon::createFromTimestampUTC(0)->utc);/*pad(60)*/)}} // {{getUTC3_eval}} 378 | 379 | // Gets the DateTimeZone instance 380 | {{get5::exec(echo get_class(Carbon::now()->timezone);/*pad(60)*/)}} // {{get5_eval}} 381 | {{get6::exec(echo get_class(Carbon::now()->tz);/*pad(60)*/)}} // {{get6_eval}} 382 | 383 | // Gets the DateTimeZone instance name, shortcut for ->timezone->getName() 384 | {{get7::exec(echo Carbon::now()->timezoneName;/*pad(60)*/)}} // {{get7_eval}} 385 | {{get8::exec(echo Carbon::now()->tzName;/*pad(60)*/)}} // {{get8_eval}} 386 | ``` 387 | 388 | 389 | ### Setters 390 | 391 | The following setters are implemented via PHP's `__set()` method. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC. 392 | 393 | ```php 394 | {{::lint( 395 | $dt = Carbon::now(); 396 | 397 | $dt->year = 1975; 398 | $dt->month = 13; // would force year++ and month = 1 399 | $dt->month = 5; 400 | $dt->day = 21; 401 | $dt->hour = 22; 402 | $dt->minute = 32; 403 | $dt->second = 5; 404 | 405 | $dt->timestamp = 169957925; // This will not change the timezone 406 | 407 | // Set the timezone via DateTimeZone instance or string 408 | $dt->timezone = new DateTimeZone('Europe/London'); 409 | $dt->timezone = 'Europe/London'; 410 | $dt->tz = 'Europe/London'; 411 | )}} 412 | ``` 413 | 414 | 415 | ### Fluent Setters 416 | 417 | No arguments are optional for the setters, but there are enough variety in the function definitions that you shouldn't need them anyway. Its good to take note here that none of the setters, with the obvious exception of explicitly setting the timezone, will change the timezone of the instance. Specifically, setting the timestamp will not set the corresponding timezone to UTC. 418 | 419 | ```php 420 | {{::lint( 421 | $dt = Carbon::now(); 422 | 423 | $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString(); 424 | $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString(); 425 | $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString(); 426 | 427 | $dt->timestamp(169957925)->timezone('Europe/London'); 428 | 429 | $dt->tz('America/Toronto')->setTimezone('America/Vancouver'); 430 | )}} 431 | ``` 432 | 433 | 434 | ### IsSet 435 | 436 | The PHP function `__isset()` is implemented. This was done as some external systems (ex. [Twig](http://twig.sensiolabs.org/doc/recipes.html#using-dynamic-object-properties)) validate the existence of a property before using it. This is done using the `isset()` or `empty()` method. You can read more about these on the PHP site: [__isset()](http://www.php.net/manual/en/language.oop5.overloading.php#object.isset), [isset()](http://www.php.net/manual/en/function.isset.php), [empty()](http://www.php.net/manual/en/function.empty.php). 437 | 438 | ```php 439 | {{isset1::exec(var_dump(isset(Carbon::now()->iDoNotExist));/*pad(50)*/)}} // {{isset1_eval}} 440 | {{isset2::exec(var_dump(isset(Carbon::now()->hour));/*pad(50)*/)}} // {{isset2_eval}} 441 | {{isset3::exec(var_dump(empty(Carbon::now()->iDoNotExist));/*pad(50)*/)}} // {{isset3_eval}} 442 | {{isset4::exec(var_dump(empty(Carbon::now()->year));/*pad(50)*/)}} // {{isset4_eval}} 443 | ``` 444 | 445 | 446 | ### String Formatting and Localization 447 | 448 | All of the available `toXXXString()` methods rely on the base class method [DateTime::format()](http://php.net/manual/en/datetime.format.php). You'll notice the `__toString()` method is defined which allows a Carbon instance to be printed as a pretty date time string when used in a string context. 449 | 450 | ```php 451 | {{::lint($dt = Carbon::create(1975, 12, 25, 14, 15, 16);)}} 452 | 453 | {{format1::exec(var_dump($dt->toDateTimeString() == $dt);/*pad(50)*/)}} // {{format1_eval}} => uses __toString() 454 | {{format2::exec(echo $dt->toDateString();/*pad(50)*/)}} // {{format2_eval}} 455 | {{format3::exec(echo $dt->toFormattedDateString();/*pad(50)*/)}} // {{format3_eval}} 456 | {{format4::exec(echo $dt->toTimeString();/*pad(50)*/)}} // {{format4_eval}} 457 | {{format5::exec(echo $dt->toDateTimeString();/*pad(50)*/)}} // {{format5_eval}} 458 | {{format6::exec(echo $dt->toDayDateTimeString();/*pad(50)*/)}} // {{format6_eval}} 459 | 460 | // ... of course format() is still available 461 | {{format7::exec(echo $dt->format('l jS \\of F Y h:i:s A');/*pad(50)*/)}} // {{format7_eval}} 462 | ``` 463 | 464 | You can also set the default __toString() format (which defaults to `Y-m-d H:i:s`). 465 | 466 | ```php 467 | {{::lint( 468 | Carbon::setDefaultFormat('jS \o\f F, Y g:i:s a'); 469 | $dt = Carbon::create(1975, 12, 25, 14, 15, 16); 470 | )}} 471 | {{format8::exec(echo $dt;/*pad(50)*/)}} // {{format8_eval}} 472 | ``` 473 | 474 | Unfortunately the base class DateTime does not have any localization support. To begin localization support a `formatLocalized($format)` method has been added. The implementation makes a call to [strftime](http://www.php.net/strftime) using the current instance timestamp. If you first set the current locale with [setlocale()](http://www.php.net/setlocale) then the string returned will be formatted in the correct locale. 475 | 476 | ```php 477 | {{::lint(setlocale(LC_TIME, 'German');/*pad(50)*/)}} 478 | {{format20::exec(echo $dt->formatLocalized('%A %d %B %Y');/*pad(50)*/)}} // {{format20_eval}} 479 | {{::lint(setlocale(LC_TIME, '');/*pad(50)*/)}} 480 | {{format21::exec(echo $dt->formatLocalized('%A %d %B %Y');/*pad(50)*/)}} // {{format21_eval}} 481 | ``` 482 | 483 | 484 | ## Common Formats 485 | 486 | The following are wrappers for the common formats provided in the [DateTime class](http://www.php.net/manual/en/class.datetime.php). 487 | 488 | ```php 489 | $dt = Carbon::now(); 490 | 491 | echo $dt->toATOMString(); // same as $dt->format(DateTime::ATOM); 492 | echo $dt->toCOOKIEString(); 493 | echo $dt->toISO8601String(); 494 | echo $dt->toRFC822String(); 495 | echo $dt->toRFC850String(); 496 | echo $dt->toRFC1036String(); 497 | echo $dt->toRFC1123String(); 498 | echo $dt->toRFC2822String(); 499 | echo $dt->toRFC3339String(); 500 | echo $dt->toRSSString(); 501 | echo $dt->toW3CString(); 502 | ``` 503 | 504 | 505 | ### Comparison 506 | 507 | Simple comparison is offered up via the following functions. Remember that the comparison is done in the UTC timezone so things aren't always as they seem. 508 | 509 | ```php 510 | {{comparetz::exec(echo Carbon::now()->tzName;/*pad(50)*/)}} // {{comparetz_eval}} 511 | {{::lint($first = Carbon::create(2012, 9, 5, 23, 26, 11);)}} 512 | {{::lint($second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');)}} 513 | 514 | {{compare1::exec(echo $first->toDateTimeString();/*pad(50)*/)}} // {{compare1_eval}} 515 | {{compare1tz::exec(echo $first->tzName;/*pad(50)*/)}} // {{compare1tz_eval}} 516 | {{compare2::exec(echo $second->toDateTimeString();/*pad(50)*/)}} // {{compare2_eval}} 517 | {{compare2tz::exec(echo $second->tzName;/*pad(50)*/)}} // {{compare2tz_eval}} 518 | 519 | {{compare3::exec(var_dump($first->eq($second));/*pad(50)*/)}} // {{compare3_eval}} 520 | {{compare4::exec(var_dump($first->ne($second));/*pad(50)*/)}} // {{compare4_eval}} 521 | {{compare5::exec(var_dump($first->gt($second));/*pad(50)*/)}} // {{compare5_eval}} 522 | {{compare6::exec(var_dump($first->gte($second));/*pad(50)*/)}} // {{compare6_eval}} 523 | {{compare7::exec(var_dump($first->lt($second));/*pad(50)*/)}} // {{compare7_eval}} 524 | {{compare8::exec(var_dump($first->lte($second));/*pad(50)*/)}} // {{compare8_eval}} 525 | 526 | {{::lint($first->setDateTime(2012, 1, 1, 0, 0, 0);)}} 527 | {{::lint($second->setDateTime(2012, 1, 1, 0, 0, 0);/*pad(50)*/)}} // Remember tz is 'America/Vancouver' 528 | 529 | {{compare9::exec(var_dump($first->eq($second));/*pad(50)*/)}} // {{compare9_eval}} 530 | {{compare10::exec(var_dump($first->ne($second));/*pad(50)*/)}} // {{compare10_eval}} 531 | {{compare11::exec(var_dump($first->gt($second));/*pad(50)*/)}} // {{compare11_eval}} 532 | {{compare12::exec(var_dump($first->gte($second));/*pad(50)*/)}} // {{compare12_eval}} 533 | {{compare13::exec(var_dump($first->lt($second));/*pad(50)*/)}} // {{compare13_eval}} 534 | {{compare14::exec(var_dump($first->lte($second));/*pad(50)*/)}} // {{compare14_eval}} 535 | ``` 536 | 537 | To determine if the current instance is between two other instances you can use the aptly named `between()` method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries. 538 | 539 | ```php 540 | {{::lint($first = Carbon::create(2012, 9, 5, 1);)}} 541 | {{::lint($second = Carbon::create(2012, 9, 5, 5);)}} 542 | {{between1::exec(var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second));/*pad(75)*/)}} // {{between1_eval}} 543 | {{between2::exec(var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second));/*pad(75)*/)}} // {{between2_eval}} 544 | {{between3::exec(var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false));/*pad(75)*/)}} // {{between3_eval}} 545 | ``` 546 | 547 | To handle the most used cases there are some simple helper functions that hopefully are obvious from their names. For the methods that compare to `now()` (ex. isToday()) in some manner the `now()` is created in the same timezone as the instance. 548 | 549 | ```php 550 | {{::lint( 551 | $dt = Carbon::now(); 552 | 553 | $dt->isWeekday(); 554 | $dt->isWeekend(); 555 | $dt->isYesterday(); 556 | $dt->isToday(); 557 | $dt->isTomorrow(); 558 | $dt->isFuture(); 559 | $dt->isPast(); 560 | $dt->isLeapYear(); 561 | )}} 562 | ``` 563 | 564 | 565 | ### Addition and Subtraction 566 | 567 | The default DateTime provides a couple of different methods for easily adding and subtracting time. There is `modify()`, `add()` and `sub()`. `modify()` takes a *magical* date/time format string, 'last day of next month', that it parses and applies the modification while `add()` and `sub()` use a `DateInterval` class thats not so obvious, `new \DateInterval('P6YT5M')`. Hopefully using these fluent functions will be more clear and easier to read after not seeing your code for a few weeks. But of course I don't make you choose since the base class functions are still available. 568 | 569 | ```php 570 | {{::lint($dt = Carbon::create(2012, 1, 31, 0);)}} 571 | 572 | {{addsub1::exec(echo $dt->toDateTimeString();/*pad(40)*/)}} // {{addsub1_eval}} 573 | 574 | {{addsub2::exec(echo $dt->addYears(5);/*pad(40)*/)}} // {{addsub2_eval}} 575 | {{addsub3::exec(echo $dt->addYear();/*pad(40)*/)}} // {{addsub3_eval}} 576 | {{addsub4::exec(echo $dt->subYear();/*pad(40)*/)}} // {{addsub4_eval}} 577 | {{addsub5::exec(echo $dt->subYears(5);/*pad(40)*/)}} // {{addsub5_eval}} 578 | 579 | {{addsub6::exec(echo $dt->addMonths(60);/*pad(40)*/)}} // {{addsub6_eval}} 580 | {{addsub7::exec(echo $dt->addMonth();/*pad(40)*/)}} // {{addsub7_eval}} equivalent of $dt->month($dt->month + 1); so it wraps 581 | {{addsub8::exec(echo $dt->subMonth();/*pad(40)*/)}} // {{addsub8_eval}} 582 | {{addsub9::exec(echo $dt->subMonths(60);/*pad(40)*/)}} // {{addsub9_eval}} 583 | 584 | {{addsub10::exec(echo $dt->addDays(29);/*pad(40)*/)}} // {{addsub10_eval}} 585 | {{addsub11::exec(echo $dt->addDay();/*pad(40)*/)}} // {{addsub11_eval}} 586 | {{addsub12::exec(echo $dt->subDay();/*pad(40)*/)}} // {{addsub12_eval}} 587 | {{addsub13::exec(echo $dt->subDays(29);/*pad(40)*/)}} // {{addsub13_eval}} 588 | 589 | {{addsub14::exec(echo $dt->addWeekdays(4);/*pad(40)*/)}} // {{addsub14_eval}} 590 | {{addsub15::exec(echo $dt->addWeekday();/*pad(40)*/)}} // {{addsub15_eval}} 591 | {{addsub16::exec(echo $dt->subWeekday();/*pad(40)*/)}} // {{addsub16_eval}} 592 | {{addsub17::exec(echo $dt->subWeekdays(4);/*pad(40)*/)}} // {{addsub17_eval}} 593 | 594 | {{addsub18::exec(echo $dt->addWeeks(3);/*pad(40)*/)}} // {{addsub18_eval}} 595 | {{addsub19::exec(echo $dt->addWeek();/*pad(40)*/)}} // {{addsub19_eval}} 596 | {{addsub20::exec(echo $dt->subWeek();/*pad(40)*/)}} // {{addsub20_eval}} 597 | {{addsub21::exec(echo $dt->subWeeks(3);/*pad(40)*/)}} // {{addsub21_eval}} 598 | 599 | {{addsub22::exec(echo $dt->addHours(24);/*pad(40)*/)}} // {{addsub22_eval}} 600 | {{addsub23::exec(echo $dt->addHour();/*pad(40)*/)}} // {{addsub23_eval}} 601 | {{addsub24::exec(echo $dt->subHour();/*pad(40)*/)}} // {{addsub24_eval}} 602 | {{addsub25::exec(echo $dt->subHours(24);/*pad(40)*/)}} // {{addsub25_eval}} 603 | 604 | {{addsub26::exec(echo $dt->addMinutes(61);/*pad(40)*/)}} // {{addsub26_eval}} 605 | {{addsub27::exec(echo $dt->addMinute();/*pad(40)*/)}} // {{addsub27_eval}} 606 | {{addsub28::exec(echo $dt->subMinute();/*pad(40)*/)}} // {{addsub28_eval}} 607 | {{addsub29::exec(echo $dt->subMinutes(61);/*pad(40)*/)}} // {{addsub29_eval}} 608 | 609 | {{addsub30::exec(echo $dt->addSeconds(61);/*pad(40)*/)}} // {{addsub30_eval}} 610 | {{addsub31::exec(echo $dt->addSecond();/*pad(40)*/)}} // {{addsub31_eval}} 611 | {{addsub32::exec(echo $dt->subSecond();/*pad(40)*/)}} // {{addsub32_eval}} 612 | {{addsub33::exec(echo $dt->subSeconds(61);/*pad(40)*/)}} // {{addsub33_eval}} 613 | ``` 614 | 615 | For fun you can also pass negative values to `addXXX()`, in fact that's how `subXXX()` is implemented. 616 | 617 | 618 | ### Difference 619 | 620 | These functions always return the **total difference** expressed in the specified time requested. This differs from the base class `diff()` function where an interval of 61 seconds would be returned as 1 minute and 1 second via a `DateInterval` instance. The `diffInMinutes()` function would simply return 1. All values are truncated and not rounded. Each function below has a default first parameter which is the Carbon instance to compare to, or null if you want to use `now()`. The 2nd parameter again is optional and indicates if you want the return value to be the absolute value or a relative value that might have a `-` (negative) sign if the passed in date is less than the current instance. This will default to true, return the absolute value. The comparisons are done in UTC. 621 | 622 | ```php 623 | // Carbon::diffInYears(Carbon $dt = null, $abs = true) 624 | 625 | {{diff1::exec(echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London'));)}} // {{diff1_eval}} 626 | 627 | {{::lint($dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');)}} 628 | {{::lint($dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');)}} 629 | {{diff4::exec(echo $dtOttawa->diffInHours($dtVancouver);/*pad(70)*/)}} // {{diff4_eval}} 630 | 631 | {{diff5::exec(echo $dtOttawa->diffInHours($dtVancouver, false);/*pad(70)*/)}} // {{diff5_eval}} 632 | {{diff6::exec(echo $dtVancouver->diffInHours($dtOttawa, false);/*pad(70)*/)}} // {{diff6_eval}} 633 | 634 | {{::lint($dt = Carbon::create(2012, 1, 31, 0);)}} 635 | {{diff8::exec(echo $dt->diffInDays($dt->copy()->addMonth());/*pad(70)*/)}} // {{diff8_eval}} 636 | {{diff9::exec(echo $dt->diffInDays($dt->copy()->subMonth(), false);/*pad(70)*/)}} // {{diff9_eval}} 637 | 638 | {{::lint($dt = Carbon::create(2012, 4, 30, 0);)}} 639 | {{diff11::exec(echo $dt->diffInDays($dt->copy()->addMonth());/*pad(70)*/)}} // {{diff11_eval}} 640 | {{diff12::exec(echo $dt->diffInDays($dt->copy()->addWeek());/*pad(70)*/)}} // {{diff12_eval}} 641 | 642 | {{::lint($dt = Carbon::create(2012, 1, 1, 0);)}} 643 | {{diff14::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(59));/*pad(70)*/)}} // {{diff14_eval}} 644 | {{diff15::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(60));/*pad(70)*/)}} // {{diff15_eval}} 645 | {{diff16::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(119));/*pad(70)*/)}} // {{diff16_eval}} 646 | {{diff17::exec(echo $dt->diffInMinutes($dt->copy()->addSeconds(120));/*pad(70)*/)}} // {{diff17_eval}} 647 | 648 | // others that are defined 649 | // diffInYears(), diffInMonths(), diffInDays() 650 | // diffInHours(), diffInMinutes(), diffInSeconds() 651 | ``` 652 | 653 | 654 | ### Difference for Humans 655 | 656 | It is easier for humans to read `1 month ago` compared to 30 days ago. This is a common function seen in most date libraries so I thought I would add it here as well. It uses approximations for a month being 4 weeks. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to `now()` if not specified. 657 | 658 | This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities: 659 | 660 | * When comparing a value in the past to default now: 661 | * 1 hour ago 662 | * 5 months ago 663 | 664 | * When comparing a value in the future to default now: 665 | * 1 hour from now 666 | * 5 months from now 667 | 668 | * When comparing a value in the past to another value: 669 | * 1 hour before 670 | * 5 months before 671 | 672 | * When comparing a value in the future to another value: 673 | * 1 hour after 674 | * 5 months after 675 | 676 | ```php 677 | // The most typical usage is for comments 678 | // The instance is the date the comment was created and its being compared to default now() 679 | {{humandiff1::exec(echo Carbon::now()->subDays(5)->diffForHumans();/*pad(62)*/)}} // {{humandiff1_eval}} 680 | 681 | {{humandiff2::exec(echo Carbon::now()->diffForHumans(Carbon::now()->subYear());/*pad(62)*/)}} // {{humandiff2_eval}} 682 | 683 | {{::lint($dt = Carbon::createFromDate(2011, 2, 1);)}} 684 | 685 | {{humandiff4::exec(echo $dt->diffForHumans($dt->copy()->addMonth());/*pad(62)*/)}} // {{humandiff4_eval}} 686 | {{humandiff5::exec(echo $dt->diffForHumans($dt->copy()->subMonth());/*pad(62)*/)}} // {{humandiff5_eval}} 687 | 688 | {{humandiff6::exec(echo Carbon::now()->addSeconds(5)->diffForHumans();/*pad(62)*/)}} // {{humandiff6_eval}} 689 | 690 | {{humandiff7::exec(echo Carbon::now()->subDays(24)->diffForHumans();/*pad(62)*/)}} // {{humandiff7_eval}} 691 | ``` 692 | 693 | 694 | ### Modifiers 695 | 696 | These group of methods perform helpful modifications to the current instance. Most of them are self explanatory from their names... or at least should be. You'll also notice that the startOfXXX(), next() and previous() methods set the time to 00:00:00 and the endOfXXX() methods set the time to 23:59:59. 697 | 698 | ```php 699 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);/*pad(40)*/)}} 700 | {{modifier1::exec(echo $dt->startOfDay();/*pad(50)*/)}} // {{modifier1_eval}} 701 | 702 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 703 | {{modifier2::exec(echo $dt->endOfDay();/*pad(50)*/)}} // {{modifier2_eval}} 704 | 705 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 706 | {{modifier3::exec(echo $dt->startOfMonth();/*pad(50)*/)}} // {{modifier3_eval}} 707 | 708 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 709 | {{modifier4::exec(echo $dt->endOfMonth();/*pad(50)*/)}} // {{modifier4_eval}} 710 | 711 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 712 | {{modifier5::exec(echo $dt->startOfWeek();/*pad(50)*/)}} // {{modifier5_eval}} 713 | {{modifier6::exec(var_dump($dt->dayOfWeek == Carbon::MONDAY);/*pad(50)*/)}} // {{modifier6_eval}} : ISO8601 week starts on Monday 714 | 715 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 716 | {{modifier7::exec(echo $dt->endOfWeek();/*pad(50)*/)}} // {{modifier7_eval}} 717 | {{modifier8::exec(var_dump($dt->dayOfWeek == Carbon::SUNDAY);/*pad(50)*/)}} // {{modifier8_eval}} : ISO8601 week ends on Sunday 718 | 719 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 720 | {{modifier9::exec(echo $dt->next(Carbon::WEDNESDAY);/*pad(50)*/)}} // {{modifier9_eval}} 721 | {{modifier10::exec(var_dump($dt->dayOfWeek == Carbon::WEDNESDAY);/*pad(50)*/)}} // {{modifier10_eval}} 722 | 723 | {{::lint($dt = Carbon::create(2012, 1, 1, 12, 0, 0);)}} 724 | {{modifier11::exec(echo $dt->next();/*pad(50)*/)}} // {{modifier11_eval}} 725 | 726 | {{::lint($dt = Carbon::create(2012, 1, 31, 12, 0, 0);)}} 727 | {{modifier12::exec(echo $dt->previous(Carbon::WEDNESDAY);/*pad(50)*/)}} // {{modifier12_eval}} 728 | {{modifier13::exec(var_dump($dt->dayOfWeek == Carbon::WEDNESDAY);/*pad(50)*/)}} // {{modifier13_eval}} 729 | 730 | {{::lint($dt = Carbon::create(2012, 1, 1, 12, 0, 0);)}} 731 | {{modifier14::exec(echo $dt->previous();/*pad(50)*/)}} // {{modifier14_eval}} 732 | 733 | // others that are defined that are similar 734 | // firstOfMonth(), lastOfMonth(), nthOfMonth() 735 | // firstOfQuarter(), lastOfQuarter(), nthOfQuarter() 736 | // firstOfYear(), lastOfYear(), nthOfYear() 737 | ``` 738 | 739 | 740 | ### Constants 741 | 742 | The following constants are defined in the Carbon class. 743 | 744 | * SUNDAY = 0 745 | * MONDAY = 1 746 | * TUESDAY = 2 747 | * WEDNESDAY = 3 748 | * THURSDAY = 4 749 | * FRIDAY = 5 750 | * SATURDAY = 6 751 | * MONTHS_PER_YEAR = 12 752 | * HOURS_PER_DAY = 24 753 | * MINUTES_PER_HOUR = 60 754 | * SECONDS_PER_MINUTE = 60 755 | 756 | ```php 757 | {{::lint( 758 | $dt = Carbon::createFromDate(2012, 10, 6); 759 | if ($dt->dayOfWeek === Carbon::SATURDAY) { 760 | echo 'Place bets on Ottawa Senators Winning!'; 761 | } 762 | )}} 763 | ``` 764 | 765 | 766 | ## About 767 | 768 | 769 | ### Contributing 770 | 771 | I hate reading a readme.md file that has code errors and/or sample output that is incorrect. I tried something new with this project and wrote a quick readme parser that can **lint** sample source code or **execute** and inject the actual result into a generated readme. 772 | 773 | > **Don't make changes to the `readme.md` directly!!** 774 | 775 | Change the `readme.src.md` and then use the `readme.php` to generate the new `readme.md` file. It can be run at the command line using `php readme.php` from the project root. Maybe someday I'll extract this out to another project or at least run it with a post receive hook, but for now its just a local tool, deal with it. 776 | 777 | The commands are quickly explained below. To see some examples you can view the raw `readme.src.md` file in this repo. 778 | 779 | `\{\{::lint()}}` 780 | 781 | The `lint` command is meant for confirming the code is valid and will `eval()` the code passed into the function. Assuming there were no errors, the executed source code will then be injected back into the text replacing out the `\{\{::lint()}}`. When you look at the raw `readme.src.md` you will see that the code can span several lines. Remember the code is executed in the context of the running script so any variables will be available for the rest of the file. 782 | 783 | \{\{::lint($var = 'brian nesbitt';)}} => {{::lint($var = 'brian nesbitt';)}} 784 | 785 | > As mentioned the `$var` can later be echo'd and you would get 'brian nesbitt' as all of the source is executed in the same scope. 786 | 787 | `\{\{varName::exec()}}` and `{{varName_eval}}` 788 | 789 | The `exec` command begins by performing an `eval()` on the code passed into the function. The executed source code will then be injected back into the text replacing out the `\{\{varName::exec()}}`. This will also create a variable named `varName_eval` that you can then place anywhere in the file and it will get replaced with the output of the `eval()`. You can use any type of output (`echo`, `printf`, `var_dump` etc) statement to return the result value as an output buffer is setup to capture the output. 790 | 791 | \{\{exVarName::exec(echo $var;)}} => {{exVarName::exec(echo $var;)}} 792 | \{\{exVarName_eval}} => {{exVarName_eval}} // $var is still set from above 793 | 794 | `/*pad()*/` 795 | 796 | The `pad()` is a special source modifier. This will pad the code block to the indicated number of characters using spaces. Its particularly handy for aligning `//` comments when showing results. 797 | 798 | \{\{exVarName1::exec(echo 12345;/*pad(20)*/)}} // \{\{exVarName1_eval}} 799 | \{\{exVarName2::exec(echo 6;/*pad(20)*/)}} // \{\{exVarName2_eval}} 800 | 801 | ... would generate to: 802 | 803 | {{exVarName1::exec(echo 12345;/*pad(20)*/)}} // {{exVarName1_eval}} 804 | {{exVarName2::exec(echo 6;/*pad(20)*/)}} // {{exVarName2_eval}} 805 | 806 | Apart from the readme the typical steps can be used to contribute your own improvements. 807 | 808 | * Fork 809 | * Clone 810 | * PHPUnit 811 | * Branch 812 | * PHPUnit 813 | * Code 814 | * PHPUnit 815 | * Commit 816 | * Push 817 | * Pull request 818 | * Relax and play Castle Crashers 819 | 820 | 821 | ### Author 822 | 823 | Brian Nesbitt - - 824 | 825 | 826 | ### License 827 | 828 | Carbon is licensed under the MIT License - see the `LICENSE` file for details 829 | 830 | 831 | ### History 832 | 833 | You can view the history of the Carbon project in the [history file](https://github.com/briannesbitt/Carbon/blob/master/history.md). 834 | 835 | 836 | ### Why the name Carbon? 837 | 838 | Read about [Carbon Dating](http://en.wikipedia.org/wiki/Radiocarbon_dating) --------------------------------------------------------------------------------