Loading... # Introduction ```php <?php namespace Carbon; class Carbon extends \DateTime { // code here } ``` ```php use Carbon\Carbon; ``` > We also provide CarbonImmutable class extending [DateTimeImmutable](http://www.php.net/manual/en/class.datetimeimmutable.php). The same methods are available on both classes but when you use a modifier on a Carbon instance, it modifies and returns the same instance, when you use it on CarbonImmutable, it returns a new instances with the new value. ```php <?php $mutable = Carbon::now(); $immutable = CarbonImmutable::now(); $modifiedMutable = $mutable->add(1, 'day'); $modifiedImmutable = CarbonImmutable::now()->add(1, 'day'); var_dump($modifiedMutable === $mutable); // bool(true) var_dump($mutable->isoFormat('dddd D')); // string(11) "Saturday 22" var_dump($modifiedMutable->isoFormat('dddd D')); // string(11) "Saturday 22" // So it means $mutable and $modifiedMutable are the same object // both set to now + 1 day. var_dump($modifiedImmutable === $immutable); // bool(false) var_dump($immutable->isoFormat('dddd D')); // string(9) "Friday 21" var_dump($modifiedImmutable->isoFormat('dddd D')); // string(11) "Saturday 22" // While $immutable is still set to now and cannot be changed and // $modifiedImmutable is a new instance created from $immutable // set to now + 1 day. $mutable = CarbonImmutable::now()->toMutable(); var_dump($mutable->isMutable()); // bool(true) var_dump($mutable->isImmutable()); // bool(false) $immutable = Carbon::now()->toImmutable(); var_dump($immutable->isMutable()); // bool(false) var_dump($immutable->isImmutable()); // bool(true) ``` Carbon has all of the functions inherited from the base DateTime class. This approach allows you to access the base functionality such as [modify](http://php.net/manual/en/datetime.modify.php), [format](http://php.net/manual/en/datetime.format.php) or [diff](http://php.net/manual/en/datetime.diff.php). ```php <?php $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); // Try to replace the 4th number (hours) or the last argument (timezone) with // Europe/Paris for example and see the actual result on the right hand. // It's alive! echo $dtVancouver->diffInHours($dtToronto); // 3 // Now, try to double-click on "diffInHours" or "create" to open // the References panel. // Once the references panel is open, you can use the search field to // filter the list or click the (<) button to close it. ``` # Instantiation ```php $carbon = new Carbon(); // equivalent to Carbon::now() $carbon = new Carbon('first day of January 2008', 'America/Vancouver'); echo get_class($carbon); // 'Carbon\Carbon' $carbon = new Carbon(new DateTime('first day of January 2008'), new DateTimeZone('America/Vancouver')); // equivalent to previous instance ``` ```php $now = Carbon::now(); // will use timezone as set with date_default_timezone_set // PS: we recommend you to work with UTC as default timezone and only use // other timezones (such as the user timezone) on display $nowInLondonTz = Carbon::now(new DateTimeZone('Europe/London')); // or just pass the timezone as a string $nowInLondonTz = Carbon::now('Europe/London'); echo $nowInLondonTz->tzName; // Europe/London echo "\n"; // or to create a date with a custom fixed timezone offset $date = Carbon::now('+13:30'); echo $date->tzName; // +13:30 echo "\n"; // Get/set minutes offset from UTC echo $date->utcOffset(); // 810 echo "\n"; $date->utcOffset(180); echo $date->tzName; // +03:00 echo "\n"; echo $date->utcOffset(); // 180 ``` 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. ```php echo (new Carbon('first day of December 2008'))->addWeeks(2); // 2008-12-15 00:00:00 echo "\n"; echo Carbon::parse('first day of December 2008')->addWeeks(2); // 2008-12-15 00:00:00 ``` The string passed to `Carbon::parse` or to `new Carbon` can represent a relative time (next sunday, tomorrow, first day of next month, last year) or an absolute time (first day of December 2008, 2017-01-06). You can test if a string will produce a relative or absolute date with `Carbon::hasRelativeKeywords()`. ```php $string = 'first day of next month'; if (strtotime($string) === false) { echo "'$string' is not a valid date/time string."; } elseif (Carbon::hasRelativeKeywords($string)) { echo "'$string' is a relative valid date/time string, it will returns different dates depending on the current date."; } else { echo "'$string' is an absolute date/time string, it will always returns the same date."; } ``` 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`. ```php $now = Carbon::now(); echo $now; // 2023-04-21 08:24:04 echo "\n"; $today = Carbon::today(); echo $today; // 2023-04-21 00:00:00 echo "\n"; $tomorrow = Carbon::tomorrow('Europe/London'); echo $tomorrow; // 2023-04-22 00:00:00 echo "\n"; $yesterday = Carbon::yesterday(); echo $yesterday; // 2023-04-20 00:00:00 ``` ```php $year = 2000; $month = 4; $day = 19; $hour = 20; $minute = 30; $second = 15; $tz = 'Europe/Madrid'; echo Carbon::createFromDate($year, $month, $day, $tz)."\n"; echo Carbon::createMidnightDate($year, $month, $day, $tz)."\n"; echo Carbon::createFromTime($hour, $minute, $second, $tz)."\n"; echo Carbon::createFromTimeString("$hour:$minute:$second", $tz)."\n"; echo Carbon::create($year, $month, $day, $hour, $minute, $second, $tz)."\n"; ``` 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. ```php $year = 2000; $month = 4; $day = 19; $hour = 20; $minute = 30; $second = 15; $tz = 'Asia/Shanghai'; echo Carbon::createFromDate($year, $month, $day, $tz) . "\n"; echo Carbon::createMidnightDate($year, $month, $day, $tz) . "\n"; echo Carbon::createFromTime($hour, $minute, $second, $tz) . "\n"; echo Carbon::createFromTimeString("$hour:$minute:$second", $tz) . "\n"; echo Carbon::create($year, $month, $day, $hour, $minute, $second, $tz) . "\n"; //2000-04-19 15:45:41 //2000-04-19 00:00:00 //2023-05-15 20:30:15 //2023-05-15 20:30:15 //2000-04-19 20:30:15 ``` `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 is for `create()` that has minimum value as default for missing argument but default on current value when you pass explicitly `null`. ```php masThisYear = Carbon::createFromDate(null, 12, 25); // Year defaults to current year $Y2K = Carbon::create(2000, 1, 1, 0, 0, 0); // equivalent to Carbon::createMidnightDate(2000, 1, 1) $alsoY2K = Carbon::create(1999, 12, 31, 24); $noonLondonTz = Carbon::createFromTime(12, 0, 0, 'Europe/London'); $teaTime = Carbon::createFromTimeString('17:00:00', 'Europe/London'); try { Carbon::create(1975, 5, 21, 22, -2, 0); } catch(InvalidArgumentException $x) { echo $x->getMessage(); } // minute must be between 0 and 99, -2 given // Be careful, as Carbon::createFromDate() default values to current date, it can trigger overflow: // For example, if we are the 15th of June 2020, the following will set the date on 15: Carbon::createFromDate(2019, 4); // 2019-04-15 // If we are the 31th of October, as 31th April does not exist, it overflows to May: Carbon::createFromDate(2019, 4); // 2019-05-01 // That's why you simply should not use Carbon::createFromDate() with only 2 parameters (1 or 3 are safe, but no 2) ``` Create exceptions occurs on such negative values but not on overflow, to get exceptions on overflow, use `createSafe()` ```php echo Carbon::create(2000, 1, 35, 13, 0, 0); // 2000-02-04 13:00:00 echo "\n"; try { Carbon::createSafe(2000, 1, 35, 13, 0, 0); } catch (\Carbon\Exceptions\InvalidDateException $exp) { echo $exp->getMessage(); } // day : 35 is not a valid value. ``` Note 1: 2018-02-29 also throws an exception while 2020-02-29 does not since 2020 is a leap year. Note 2: `Carbon::createSafe(2014, 3, 30, 1, 30, 0, 'Europe/London')` also produces an exception as this time is in an hour skipped by the daylight saving time. Note 3: The PHP native API allow consider there is a year `0` between `-1` and `1` even if it doesn't regarding to Gregorian calendar. That's why years lower than 1 will throw an exception using `createSafe`. Check [isValid()](https://carbon.nesbot.com/docs/#doc-method-Carbon-isValid) for year-0 detection. ```php Carbon::createFromFormat($format, $time, $tz); ``` `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. ```php echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00 ``` You can test if a date matches a format for `createFromFormat()` (e.g. date/time components, modifiers or separators) using `Carbon::hasFormatWithModifiers()` or `Carbon::canBeCreatedFromFormat()` which also ensure data is actually enough to create an instance. ```php var_dump(Carbon::hasFormatWithModifiers('21/05/1975', 'd#m#Y!')); // bool(true) // As 21 is too high for a month number and day is expected to be formatted "05": var_dump(Carbon::hasFormatWithModifiers('5/21/1975', 'd#m#Y!')); // bool(false) // 5 is ok for N format: var_dump(Carbon::hasFormatWithModifiers('5', 'N')); // bool(true) // but not enough to create an instance: var_dump(Carbon::canBeCreatedFromFormat('5', 'N')); // bool(false) // Both hasFormatWithModifiers() and hasFormat() exist because // hasFormat() does not interpret modifiers, it checks strictly if ->format() could have produce the given // string with the given format: var_dump(Carbon::hasFormat('21/05/1975', 'd#m#Y!')); // bool(false) var_dump(Carbon::hasFormat('21#05#1975!', 'd#m#Y!')); // bool(true) ``` You can create instances from [unix timestamps](http://en.wikipedia.org/wiki/Unix_time). `createFromTimestamp()` create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone. `createFromTimestampUTC()`, is different in that the timezone will remain UTC (GMT+00:00), it's equivalent to `Carbon::parse('@'.$timestamp)` but it supports int, float or string containing one or more numbers (like the one produced by `microtime()`) so it can also set microseconds with no precision lost. The third, `createFromTimestampMs()`, accepts a timestamp in milliseconds instead of seconds. Negative timestamps are also allowed. ```php echo Carbon::createFromTimestamp(-1)->toDateTimeString(); // 1969-12-31 23:59:59 echo Carbon::createFromTimestamp(-1.5, 'Europe/London')->toDateTimeString(); // 1970-01-01 00:59:58 echo Carbon::createFromTimestampUTC(-1)->toDateTimeString(); // 1969-12-31 23:59:59 echo Carbon::createFromTimestamp('1601735792.198956', 'Europe/London')->format('Y-m-d\TH:i:s.uP'); // 2020-10-03T15:36:32.198956+01:00 echo Carbon::createFromTimestampUTC('0.198956 1601735792')->format('Y-m-d\TH:i:s.uP'); // 2020-10-03T14:36:32.198956+00:00 echo Carbon::createFromTimestampMs(1)->format('Y-m-d\TH:i:s.uP'); // 1970-01-01T00:00:00.001000+00:00 echo Carbon::createFromTimestampMs('1601735792198.956', 'Europe/London')->format('Y-m-d\TH:i:s.uP'); // 2020-10-03T15:36:32.198956+01:00 echo Carbon::createFromTimestampMsUTC('0.956 1601735792198')->format('Y-m-d\TH:i:s.uP'); // 2020-10-03T14:36:32.198956+00:00 ``` 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. ```php $dt = Carbon::now(); echo $dt->diffInYears($dt->copy()->addYear()); // 1 // $dt was unchanged and still holds the value of Carbon:now() // Without ->copy() it would return 0 because addYear() modify $dt so // diffInYears() compare $dt with itself: echo $dt->diffInYears($dt->addYear()); // 0 // Note that this would not happen neither with CarbonImmutable // When immutable, any add/sub methods return a new instance: $dt = CarbonImmutable::now(); echo $dt->diffInYears($dt->addYear()); // 1 // Last, when your variable can be either a Carbon or CarbonImmutable, // You can use avoidMutation() which will copy() only if the given date // is mutable: echo $dt->diffInYears($dt->avoidMutation()->addYear()); // 1 ``` You can use `nowWithSameTz()` on an existing Carbon instance to get a new instance at now in the same timezone. ```php $meeting = Carbon::createFromTime(19, 15, 00, 'Africa/Johannesburg'); // 19:15 in Johannesburg echo 'Meeting starts at '.$meeting->format('H:i').' in Johannesburg.'; // Meeting starts at 19:15 in Johannesburg. // now in Johannesburg echo "It's ".$meeting->nowWithSameTz()->format('H:i').' right now in Johannesburg.'; // It's 10:24 right now in Johannesburg. ``` Finally, if you find yourself inheriting a `\DateTime` instance from another library, fear not! You can create a `Carbon` instance via a friendly `instance()` method. Or use the even more flexible method `make()` which can return a new Carbon instance from a DateTime, Carbon or from a string, else it just returns null. ````php $dt = new \DateTime('first day of January 2008'); // <== instance from another API $carbon = Carbon::instance($dt); echo get_class($carbon); // 'Carbon\Carbon' echo $carbon->toDateTimeString(); // 2008-01-01 00:00:00 ```` Ever need to loop through some dates to find the earliest or latest date? Didn't know what to set your initial maximum/minimum values to? There are now two helpers for this to make your decision simple: ```php echo Carbon::maxValue(); // '9999-12-31 23:59:59' echo Carbon::minValue(); // '0001-01-01 00:00:00' ``` # Getters 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. ```php $dt = Carbon::parse('2012-10-5 23:26:11.123789'); // These getters specifically return integers, ie intval() var_dump($dt->year); // int(2012) var_dump($dt->month); // int(10) var_dump($dt->day); // int(5) var_dump($dt->hour); // int(23) var_dump($dt->minute); // int(26) var_dump($dt->second); // int(11) var_dump($dt->micro); // int(123789) // dayOfWeek returns a number between 0 (sunday) and 6 (saturday) var_dump($dt->dayOfWeek); // int(5) // dayOfWeekIso returns a number between 1 (monday) and 7 (sunday) var_dump($dt->dayOfWeekIso); // int(5) var_dump($dt->englishDayOfWeek); // string(6) "Friday" var_dump($dt->shortEnglishDayOfWeek); // string(3) "Fri" var_dump($dt->locale('de')->dayName); // string(7) "Freitag" var_dump($dt->locale('de')->shortDayName); // string(3) "Fr." var_dump($dt->locale('de')->minDayName); // string(2) "Fr" var_dump($dt->englishMonth); // string(7) "October" var_dump($dt->shortEnglishMonth); // string(3) "Oct" var_dump($dt->locale('de')->monthName); // string(7) "Oktober" var_dump($dt->locale('de')->shortMonthName); // string(3) "Okt" // Following are deprecated, locale* and shortLocale* properties // are translated using formatLocalized() based on LC_TIME language. setlocale(LC_TIME, 'German'); var_dump($dt->localeDayOfWeek); // Deprecated: Function strftime() is deprecated in C:\Users\kylek\PhpstormProjects\CarbonDoc\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1894 string(7) "Freitag" var_dump($dt->shortLocaleDayOfWeek); // Deprecated: Function strftime() is deprecated in C:\Users\kylek\PhpstormProjects\CarbonDoc\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1894 string(2) "Fr" var_dump($dt->localeMonth); // Deprecated: Function strftime() is deprecated in C:\Users\kylek\PhpstormProjects\CarbonDoc\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1894 string(7) "Oktober" var_dump($dt->shortLocaleMonth); // Deprecated: Function strftime() is deprecated in C:\Users\kylek\PhpstormProjects\CarbonDoc\vendor\nesbot\carbon\src\Carbon\Traits\Date.php on line 1894 string(3) "Okt" setlocale(LC_TIME, ''); var_dump($dt->dayOfYear); // int(279) var_dump($dt->weekNumberInMonth); // int(1) // weekNumberInMonth consider weeks from monday to sunday, so the week 1 will // contain 1 day if the month start with a sunday, and up to 7 if it starts with a monday var_dump($dt->weekOfMonth); // int(1) // weekOfMonth will returns 1 for the 7 first days of the month, then 2 from the 8th to // the 14th, 3 from the 15th to the 21st, 4 from 22nd to 28th and 5 above var_dump($dt->weekOfYear); // int(40) var_dump($dt->daysInMonth); // int(31) var_dump($dt->timestamp); // int(1349479571) var_dump($dt->getTimestamp()); // int(1349479571) // Millisecond-precise timestamp as int var_dump($dt->getTimestampMs()); // int(1349479571124) // Millisecond-precise timestamp as float (useful to pass it to JavaScript) var_dump($dt->valueOf()); // float(1349479571124) // Custom-precision timestamp var_dump($dt->getPreciseTimestamp(6)); // float(1349479571123789) var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(47) calculated vs now in the same tz var_dump($dt->quarter); // int(4) // Returns an int of seconds difference from UTC (+/- sign included) var_dump(Carbon::createFromTimestampUTC(0)->offset); // int(0) var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->offset); // int(3600) var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->getOffset()); // int(3600) // Returns an int of hours difference from UTC (+/- sign included) var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->offsetMinutes); // int(60) var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->offsetHours); // int(1) // Returns timezone offset as string var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->getOffsetString()); // string(6) "+01:00" // Returns timezone as CarbonTimeZone var_dump(Carbon::createFromTimestamp(0, 'Europe/Paris')->getTimezone()); /* object(Carbon\CarbonTimeZone)#12335 (2) { ["timezone_type"]=> int(3) ["timezone"]=> string(12) "Europe/Paris" } */ // Indicates if day light savings time is on var_dump(Carbon::createFromDate(2012, 1, 1)->dst); // bool(false) var_dump(Carbon::createFromDate(2012, 9, 1)->dst); // bool(false) var_dump(Carbon::createFromDate(2012, 9, 1)->isDST()); // bool(false) // Indicates if the instance is in the same timezone as the local timezone var_dump(Carbon::now()->local); // bool(true) var_dump(Carbon::now('America/Vancouver')->local); // bool(false) var_dump(Carbon::now()->isLocal()); // bool(true) var_dump(Carbon::now('America/Vancouver')->isLocal()); // bool(false) var_dump(Carbon::now()->isUtc()); // bool(true) var_dump(Carbon::now('America/Vancouver')->isUtc()); // bool(false) // can also be written ->isUTC() // Indicates if the instance is in the UTC timezone var_dump(Carbon::now()->utc); // bool(true) // London is not UTC on summer time var_dump(Carbon::parse('2018-10-01', 'Europe/London')->utc); // bool(false) // London is UTC on winter time var_dump(Carbon::parse('2018-11-01', 'Europe/London')->utc); // bool(true) var_dump(Carbon::createFromTimestampUTC(0)->utc); // bool(true) // Gets the DateTimeZone instance echo get_class(Carbon::now()->timezone); // Carbon\CarbonTimeZone echo "\n"; echo get_class(Carbon::now()->tz); // Carbon\CarbonTimeZone echo "\n"; // Gets the DateTimeZone instance name, shortcut for ->timezone->getName() echo Carbon::now()->timezoneName; // UTC echo "\n"; echo Carbon::now()->tzName; // UTC echo "\n"; // You can get any property dynamically too: $unit = 'second'; var_dump(Carbon::now()->get($unit)); // int(5) // equivalent to: var_dump(Carbon::now()->$unit); // int(5) // If you have plural unit name, use singularUnit() $unit = Carbon::singularUnit('seconds'); var_dump(Carbon::now()->get($unit)); // int(5) // Prefer using singularUnit() because some plurals are not the word with S: var_dump(Carbon::pluralUnit('century')); // string(9) "centuries" var_dump(Carbon::pluralUnit('millennium')); // string(9) "millennia" ``` # Setters 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. ```php $dt = Carbon::now(); $dt->year = 1975; $dt->month = 13; // would force year++ and month = 1 $dt->month = 5; $dt->day = 21; $dt->hour = 22; $dt->minute = 32; $dt->second = 5; $dt->timestamp = 169957925; // This will not change the timezone // Same as: $dt->setTimestamp(169957925); $dt->timestamp(169957925); // Set the timezone via DateTimeZone instance or string $dt->tz = new DateTimeZone('Europe/London'); $dt->tz = 'Europe/London'; // The ->timezone is also available for backward compatibility but // it will be overridden by native php DateTime class as soon as // the object is dump (passed foreach, serialize, var_export, clone, etc.) // making the Carbon setter inefficient, if it happen, you can cleanup // those overridden properties by calling ->cleanupDumpProperties() on // the instance, but we rather recommend to simply use ->tz instead // of ->timezone everywhere. // verbose way: $dt->setYear(2001); echo $dt->year; // 2001 echo "\n"; // set/get method: $dt->year(2002); echo $dt->year(); // 0000-05-22 03:32:05 echo "\n"; // dynamic way: $dt->set('year', 2003); echo $dt->get('year'); // 2003 echo "\n"; // these methods exist for every units even for calculated properties such as: echo $dt->dayOfYear(35)->format('Y-m-d'); // 2003-02-04 ``` # Weeks ```php $en = CarbonImmutable::now()->locale('en_US'); $ar = CarbonImmutable::now()->locale('ar'); var_dump($en->firstWeekDay); // int(0) var_dump($en->lastWeekDay); // int(6) var_dump($en->startOfWeek()->format('Y-m-d H:i')); // string(16) "2023-04-16 00:00" var_dump($en->endOfWeek()->format('Y-m-d H:i')); // string(16) "2023-04-22 23:59" echo "-----------\n"; // We still can force to use an other day as start/end of week $start = $en->startOfWeek(Carbon::TUESDAY); $end = $en->endOfWeek(Carbon::MONDAY); var_dump($start->format('Y-m-d H:i')); // string(16) "2023-04-18 00:00" var_dump($end->format('Y-m-d H:i')); // string(16) "2023-04-24 23:59" echo "-----------\n"; var_dump($ar->firstWeekDay); // int(6) var_dump($ar->lastWeekDay); // int(5) var_dump($ar->startOfWeek()->format('Y-m-d H:i')); // string(16) "2023-04-15 00:00" var_dump($ar->endOfWeek()->format('Y-m-d H:i')); // string(16) "2023-04-21 23:59" $en = CarbonImmutable::parse('2015-02-05'); // use en_US as default locale echo "-----------\n"; var_dump($en->weeksInYear()); // int(52) var_dump($en->isoWeeksInYear()); // int(53) $en = CarbonImmutable::parse('2017-02-05'); echo "-----------\n"; var_dump($en->week()); // int(6) var_dump($en->isoWeek()); // int(5) var_dump($en->week(1)->format('Y-m-d H:i')); // string(16) "2017-01-01 00:00" var_dump($en->isoWeek(1)->format('Y-m-d H:i')); // string(16) "2017-01-08 00:00" // weekday/isoWeekday are meant to be used with days constants var_dump($en->weekday()); // int(0) var_dump($en->isoWeekday()); // int(7) var_dump($en->weekday(CarbonInterface::WEDNESDAY) ->format('Y-m-d H:i')); // string(16) "2017-02-08 00:00" var_dump($en->isoWeekday(CarbonInterface::WEDNESDAY) ->format('Y-m-d H:i')); // string(16) "2017-02-01 00:00" // getDaysFromStartOfWeek/setDaysFromStartOfWeek return and take a number of days // taking the current locale into account $date = CarbonImmutable::parse('2022-12-05')->locale('en_US'); var_dump($date->getDaysFromStartOfWeek()); // int(1) $date = CarbonImmutable::parse('2022-12-05')->locale('de_AT'); var_dump($date->getDaysFromStartOfWeek()); // int(0) var_dump($date->setDaysFromStartOfWeek(3)->format('Y-m-d')); // string(10) "2022-12-08" // Or specify explicitly the first day of week var_dump($date->setDaysFromStartOfWeek(3, CarbonInterface::SUNDAY)->format('Y-m-d')); // string(10) "2022-12-07" $en = CarbonImmutable::parse('2017-01-01'); echo "-----------\n"; var_dump($en->weekYear()); // int(2017) var_dump($en->isoWeekYear()); // int(2016) var_dump($en->weekYear(2016)->format('Y-m-d H:i')); // string(16) "2015-12-27 00:00" var_dump($en->isoWeekYear(2016)->format('Y-m-d H:i')); // string(16) "2017-01-01 00:00" var_dump($en->weekYear(2015)->format('Y-m-d H:i')); // string(16) "2014-12-28 00:00" var_dump($en->isoWeekYear(2015)->format('Y-m-d H:i')); // string(16) "2015-12-27 00:00" // Note you still can force first day of week and year to use: $en = CarbonImmutable::parse('2017-01-01'); echo "-----------\n"; var_dump($en->weeksInYear(null, 6, 12)); // int(52) var_dump($en->isoWeeksInYear(null, 6, 12)); // int(52) var_dump($en->week(null, 6, 12)); // int(52) var_dump($en->isoWeek(null, 6, 12)); // int(52) var_dump($en->weekYear(null, 6, 12)); // int(2016) var_dump($en->isoWeekYear(null, 6, 12)); // int(2016) var_dump($en->weekYear(2016, 6, 12)->format('Y-m-d H:i')); // string(16) "2017-01-01 00:00" var_dump($en->isoWeekYear(2016, 6, 12)->format('Y-m-d H:i')); // string(16) "2017-01-01 00:00" // Then you can see using a method or its ISO variant return identical results ``` # Fluent Setters You can call any base unit as a setter or some grouped setters: ```php $dt = Carbon::now(); $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString(); $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString(); $dt->setDate(1975, 5, 21)->setTimeFromTimeString('22:32:05')->toDateTimeString(); $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString(); // All allow microsecond as optional argument $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->microsecond(123456)->toDateTimeString(); $dt->setDate(1975, 5, 21)->setTime(22, 32, 5, 123456)->toDateTimeString(); $dt->setDate(1975, 5, 21)->setTimeFromTimeString('22:32:05.123456')->toDateTimeString(); $dt->setDateTime(1975, 5, 21, 22, 32, 5, 123456)->toDateTimeString(); $dt->timestamp(169957925); // Note: timestamps are UTC but do not change the date timezone $dt->timezone('Europe/London')->tz('America/Toronto')->setTimezone('America/Vancouver'); ``` You also can set date and time separately from other DateTime/Carbon objects: ```php $source1 = new Carbon('2010-05-16 22:40:10.1'); $dt = new Carbon('2001-01-01 01:01:01.2'); $dt->setTimeFrom($source1); echo $dt; // 2001-01-01 22:40:10 $source2 = new DateTime('2013-09-01 09:22:56.2'); $dt->setDateFrom($source2); echo $dt; // 2013-09-01 22:40:10 $dt->setDateTimeFrom($source2); // set date and time including microseconds // bot not settings as locale, timezone, options. ``` # IsSet ```php var_dump(isset(Carbon::now()->iDoNotExist)); // bool(false) var_dump(isset(Carbon::now()->hour)); // bool(true) var_dump(empty(Carbon::now()->iDoNotExist)); // bool(true) var_dump(empty(Carbon::now()->year)); // bool(false) ``` # String Formatting 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. ```php $dt = Carbon::create(1975, 12, 25, 14, 15, 16); var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString() echo $dt->toDateString(); // 1975-12-25 echo $dt->toFormattedDateString(); // Dec 25, 1975 echo $dt->toFormattedDayDateString(); // Thu, Dec 25, 1975 echo $dt->toTimeString(); // 14:15:16 echo $dt->toDateTimeString(); // 1975-12-25 14:15:16 echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM // ... of course format() is still available echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM // The reverse hasFormat method allows you to test if a string looks like a given format var_dump(Carbon::hasFormat('Thursday 25th December 1975 02:15:16 PM', 'l jS F Y h:i:s A')); // bool(true) ``` You can also set the default __toString() format (which defaults to `Y-m-d H:i:s`) thats used when [type juggling](http://php.net/manual/en/language.types.type-juggling.php) occurs. ```php echo $dt; // 1975-12-25 14:15:16 echo "\n"; $dt->settings([ 'toStringFormat' => 'jS \o\f F, Y g:i:s a', ]); echo $dt; // 25th of December, 1975 2:15:16 pm // As any setting, you can get the current value for a given date using: var_dump($dt->getSettings()); /* array(3) { ["toStringFormat"]=> string(20) "jS \o\f F, Y g:i:s a" ["locale"]=> string(2) "en" ["timezone"]=> string(3) "UTC" }*/ ``` As part of the settings `'toStringFormat'` can be used in factories too. It also may be a closure, so you can run any code on string cast. If you use Carbon 1 or want to apply it globally as default format, you can use: ```php $dt = Carbon::create(1975, 12, 25, 14, 15, 16); Carbon::setToStringFormat('jS \o\f F, Y g:i:s a'); echo $dt; // 25th of December, 1975 2:15:16 pm echo "\n"; Carbon::resetToStringFormat(); echo $dt; // 1975-12-25 14:15:16 ``` Note: For localization support see the [Localization](https://carbon.nesbot.com/docs/#api-localization) section. # Common Formats The following are wrappers for the common formats provided in the [DateTime class](http://www.php.net/manual/en/class.datetime.php). ```php $dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584'); // $dt->toAtomString() is the same as $dt->format(DateTime::ATOM); echo $dt->toAtomString(); // 2019-02-01T03:45:27+00:00 echo $dt->toCookieString(); // Friday, 01-Feb-2019 03:45:27 UTC echo $dt->toIso8601String(); // 2019-02-01T03:45:27+00:00 // Be aware we chose to use the full-extended format of the ISO 8601 norm // Natively, DateTime::ISO8601 format is not compatible with ISO-8601 as it // is explained here in the PHP documentation: // https://php.net/manual/class.datetime.php#datetime.constants.iso8601 // We consider it as a PHP mistake and chose not to provide method for this // format, but you still can use it this way: echo $dt->format(DateTime::ISO8601); // 2019-02-01T03:45:27+0000 echo $dt->toISOString(); // 2019-02-01T03:45:27.612584Z echo $dt->toJSON(); // 2019-02-01T03:45:27.612584Z echo $dt->toIso8601ZuluString(); // 2019-02-01T03:45:27Z echo $dt->toDateTimeLocalString(); // 2019-02-01T03:45:27 echo $dt->toRfc822String(); // Fri, 01 Feb 19 03:45:27 +0000 echo $dt->toRfc850String(); // Friday, 01-Feb-19 03:45:27 UTC echo $dt->toRfc1036String(); // Fri, 01 Feb 19 03:45:27 +0000 echo $dt->toRfc1123String(); // Fri, 01 Feb 2019 03:45:27 +0000 echo $dt->toRfc2822String(); // Fri, 01 Feb 2019 03:45:27 +0000 echo $dt->toRfc3339String(); // 2019-02-01T03:45:27+00:00 echo $dt->toRfc7231String(); // Fri, 01 Feb 2019 03:45:27 GMT echo $dt->toRssString(); // Fri, 01 Feb 2019 03:45:27 +0000 echo $dt->toW3cString(); // 2019-02-01T03:45:27+00:00 ``` # Conversion ```php $dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584'); var_dump($dt->toArray()); /* array(12) { ["year"]=> int(2019) ["month"]=> int(2) ["day"]=> int(1) ["dayOfWeek"]=> int(5) ["dayOfYear"]=> int(32) ["hour"]=> int(3) ["minute"]=> int(45) ["second"]=> int(27) ["micro"]=> int(612584) ["timestamp"]=> int(1548992727) ["formatted"]=> string(19) "2019-02-01 03:45:27" ["timezone"]=> object(Carbon\CarbonTimeZone)#12339 (2) { ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } } */ var_dump($dt->toObject()); /* object(stdClass)#12339 (12) { ["year"]=> int(2019) ["month"]=> int(2) ["day"]=> int(1) ["dayOfWeek"]=> int(5) ["dayOfYear"]=> int(32) ["hour"]=> int(3) ["minute"]=> int(45) ["second"]=> int(27) ["micro"]=> int(612584) ["timestamp"]=> int(1548992727) ["formatted"]=> string(19) "2019-02-01 03:45:27" ["timezone"]=> object(Carbon\CarbonTimeZone)#12335 (2) { ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } } */ var_dump($dt->toDate()); // Same as $dt->toDateTime() /* object(DateTime)#12339 (3) { ["date"]=> string(26) "2019-02-01 03:45:27.612584" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } */ // Note than both Carbon and CarbonImmutable can be cast // to both DateTime and DateTimeImmutable var_dump($dt->toDateTimeImmutable()); /* object(DateTimeImmutable)#12339 (3) { ["date"]=> string(26) "2019-02-01 03:45:27.612584" ["timezone_type"]=> int(3) ["timezone"]=> string(3) "UTC" } */ class MySubClass extends Carbon {} // MySubClass can be any class implementing CarbonInterface or a public static instance() method. $copy = $dt->cast(MySubClass::class); // Since 2.23.0, cast() can also take as argument any class that extend DateTime or DateTimeImmutable echo get_class($copy).': '.$copy; // Same as MySubClass::instance($dt) /* MySubClass: 2019-02-01 03:45:27 */ ``` You can use the method `carbonize` to transform many things into a `Carbon` instance based on a given source instance used as reference on need. It returns a new instance. ```php $dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-02-01 03:45:27.612584', 'Europe/Paris'); // Can take a date string and will apply the timezone from reference object var_dump($dt->carbonize('2019-03-21')); /* array(2) { ["date"]=> string(26) "2019-03-21 00:00:00.000000" ["timezone"]=> string(12) "Europe/Paris" } */ // If you pass a DatePeriod or CarbonPeriod, it will copy the period start var_dump($dt->carbonize(CarbonPeriod::create('2019-12-10', '2020-01-05'))); /* array(2) { ["date"]=> string(26) "2019-12-10 00:00:00.000000" ["timezone"]=> string(3) "UTC" } */ // If you pass a DateInterval or CarbonInterval, it will add the interval to // the reference object var_dump($dt->carbonize(CarbonInterval::days(3))); /* array(2) { ["date"]=> string(26) "2019-02-04 03:45:27.612584" ["timezone"]=> string(12) "Europe/Paris" } */ ``` # Comparison ```php echo Carbon::now()->tzName; // UTC $first = Carbon::create(2012, 9, 5, 23, 26, 11); $second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver'); echo $first->toDateTimeString(); // 2012-09-05 23:26:11 echo $first->tzName; // UTC echo $second->toDateTimeString(); // 2012-09-05 20:26:11 echo $second->tzName; // America/Vancouver var_dump($first->equalTo($second)); // bool(false) // equalTo is also available on CarbonInterval and CarbonPeriod var_dump($first->notEqualTo($second)); // bool(true) // notEqualTo is also available on CarbonInterval and CarbonPeriod var_dump($first->greaterThan($second)); // bool(false) // greaterThan is also available on CarbonInterval var_dump($first->greaterThanOrEqualTo($second)); // bool(false) // greaterThanOrEqualTo is also available on CarbonInterval var_dump($first->lessThan($second)); // bool(true) // lessThan is also available on CarbonInterval var_dump($first->lessThanOrEqualTo($second)); // bool(true) // lessThanOrEqualTo is also available on CarbonInterval $first->setDateTime(2012, 1, 1, 0, 0, 0); $second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver' var_dump($first->equalTo($second)); // bool(false) var_dump($first->notEqualTo($second)); // bool(true) var_dump($first->greaterThan($second)); // bool(false) var_dump($first->greaterThanOrEqualTo($second)); // bool(false) var_dump($first->lessThan($second)); // bool(true) var_dump($first->lessThanOrEqualTo($second)); // bool(true) // All have short hand aliases and PHP equivalent code: var_dump($first->eq($second)); // bool(false) var_dump($first->equalTo($second)); // bool(false) var_dump($first == $second); // bool(false) var_dump($first->ne($second)); // bool(true) var_dump($first->notEqualTo($second)); // bool(true) var_dump($first != $second); // bool(true) var_dump($first->gt($second)); // bool(false) var_dump($first->greaterThan($second)); // bool(false) var_dump($first->isAfter($second)); // bool(false) var_dump($first > $second); // bool(false) var_dump($first->gte($second)); // bool(false) var_dump($first->greaterThanOrEqualTo($second)); // bool(false) var_dump($first >= $second); // bool(false) var_dump($first->lt($second)); // bool(true) var_dump($first->lessThan($second)); // bool(true) var_dump($first->isBefore($second)); // bool(true) var_dump($first < $second); // bool(true) var_dump($first->lte($second)); // bool(true) var_dump($first->lessThanOrEqualTo($second)); // bool(true) var_dump($first <= $second); // bool(true) ``` Those methods use natural comparisons offered by PHP `$date1 == $date2` so all of them will ignore milli/micro-seconds before PHP 7.1, then take them into account starting with 7.1. To determine if the current instance is between two other instances you can use the aptly named `between()` method (or `isBetween()` alias). 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. ```php $first = Carbon::create(2012, 9, 5, 1); $second = Carbon::create(2012, 9, 5, 5); var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true) var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true) var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false) var_dump(Carbon::create(2012, 9, 5, 5)->isBetween($first, $second, false)); // bool(false) // Rather than passing false as a third argument, you can use betweenIncluded and betweenExcluded var_dump(Carbon::create(2012, 9, 5, 5)->betweenIncluded($first, $second)); // bool(true) var_dump(Carbon::create(2012, 9, 5, 5)->betweenExcluded($first, $second)); // bool(false) // All those methods are also available on CarbonInterval ``` Woah! Did you forget min() and max() ? Nope. That is covered as well by the suitably named `min()` and `max()` methods or `minimum()` and `maximum()` aliases. As usual the default parameter is now if null is specified. ```php $dt1 = Carbon::createMidnightDate(2012, 1, 1); $dt2 = Carbon::createMidnightDate(2014, 1, 30); echo $dt1->min($dt2); // 2012-01-01 00:00:00 echo $dt1->minimum($dt2); // 2012-01-01 00:00:00 // Also works with string echo $dt1->minimum('2014-01-30'); // 2012-01-01 00:00:00 $dt1 = Carbon::createMidnightDate(2012, 1, 1); $dt2 = Carbon::createMidnightDate(2014, 1, 30); echo $dt1->max($dt2); // 2014-01-30 00:00:00 echo $dt1->maximum($dt2); // 2014-01-30 00:00:00 // now is the default param $dt1 = Carbon::createMidnightDate(2000, 1, 1); echo $dt1->max(); // 2023-04-21 08:24:05 echo $dt1->maximum(); // 2023-04-21 08:24:05 // Remember min and max PHP native function work fine with dates too: echo max(Carbon::create('2002-03-15'), Carbon::create('2003-01-07'), Carbon::create('2002-08-25')); // 2003-01-07 00:00:00 echo min(Carbon::create('2002-03-15'), Carbon::create('2003-01-07'), Carbon::create('2002-08-25')); // 2002-03-15 00:00:00 // This way you can pass as many dates as you want and get no ambiguities about parameters order $dt1 = Carbon::createMidnightDate(2010, 4, 1); $dt2 = Carbon::createMidnightDate(2010, 3, 28); $dt3 = Carbon::createMidnightDate(2010, 4, 16); // returns the closest of two date (no matter before or after) echo $dt1->closest($dt2, $dt3); // 2010-03-28 00:00:00 echo $dt2->closest($dt1, $dt3); // 2010-04-01 00:00:00 echo $dt3->closest($dt2, $dt1); // 2010-04-01 00:00:00 // returns the farthest of two date (no matter before or after) echo $dt1->farthest($dt2, $dt3); // 2010-04-16 00:00:00 echo $dt2->farthest($dt1, $dt3); // 2010-04-16 00:00:00 echo $dt3->farthest($dt2, $dt1); // 2010-03-28 00:00:00 ``` 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. ```php $dt = Carbon::now(); $dt2 = Carbon::createFromDate(1987, 4, 23); $dt->isSameAs('w', $dt2); // w is the date of the week, so this will return true if $dt and $dt2 // the same day of week (both monday or both sunday, etc.) // you can use any format and combine as much as you want. $dt->isFuture(); $dt->isPast(); $dt->isSameYear($dt2); $dt->isCurrentYear(); $dt->isNextYear(); $dt->isLastYear(); $dt->isLongIsoYear(); // see https://en.wikipedia.org/wiki/ISO_8601#Week_dates Carbon::create(2015)->isLongYear(); // isLongIsoYear() check a given date, // while isLongYear() will ignore month/day and just check a given year number $dt->isLeapYear(); $dt->isSameQuarter($dt2); // same quarter of the same year of the given date $dt->isSameQuarter($dt2, false); // same quarter (3 months) no matter the year of the given date $dt->isCurrentQuarter(); $dt->isNextQuarter(); // date is in the next quarter $dt->isLastQuarter(); // in previous quarter $dt->isSameMonth($dt2); // same month of the same year of the given date $dt->isSameMonth($dt2, false); // same month no matter the year of the given date $dt->isCurrentMonth(); $dt->isNextMonth(); $dt->isLastMonth(); $dt->isWeekday(); $dt->isWeekend(); $dt->isMonday(); $dt->isTuesday(); $dt->isWednesday(); $dt->isThursday(); $dt->isFriday(); $dt->isSaturday(); $dt->isSunday(); $dt->isDayOfWeek(Carbon::SATURDAY); // is a saturday $dt->isLastOfMonth(); // is the last day of the month $dt->is('Sunday'); $dt->is('June'); $dt->is('2019'); $dt->is('12:23'); $dt->is('2 June 2019'); $dt->is('06-02'); $dt->isSameDay($dt2); // Same day of same month of same year $dt->isCurrentDay(); $dt->isYesterday(); $dt->isToday(); $dt->isTomorrow(); $dt->isNextWeek(); $dt->isLastWeek(); $dt->isSameHour($dt2); $dt->isCurrentHour(); $dt->isSameMinute($dt2); $dt->isCurrentMinute(); $dt->isSameSecond($dt2); $dt->isCurrentSecond(); $dt->isStartOfDay(); // check if hour is 00:00:00 $dt->isMidnight(); // check if hour is 00:00:00 (isStartOfDay alias) $dt->isEndOfDay(); // check if hour is 23:59:59 $dt->isMidday(); // check if hour is 12:00:00 (or other midday hour set with Carbon::setMidDayAt()) $born = Carbon::createFromDate(1987, 4, 23); $noCake = Carbon::createFromDate(2014, 9, 26); $yesCake = Carbon::createFromDate(2014, 4, 23); $overTheHill = Carbon::now()->subYears(50); var_dump($born->isBirthday($noCake)); // bool(false) var_dump($born->isBirthday($yesCake)); // bool(true) var_dump($overTheHill->isBirthday()); // bool(true) -> default compare it to today! // isCurrentX, isSameX, isNextX and isLastX are available for each unit ``` # Addition and Subtraction The default DateTime provides a couple of different methods for easily adding and subtracting time. There is `modify()`, `add()` and `sub()`. `change()` is an enhanced version of `modify()` that can take *magical* date/time format string, `'last day of next month'`, that it parses and applies the modification while `add()` and `sub()` can take the same the same kind of string, intervals (`DateInterval` or `CarbonInterval`) or count+unit parameters. But you can still access the native methods of `DateTime` class using `rawAdd()` and `rawSub()`. ```php $dt = Carbon::create(2012, 1, 31, 0); echo $dt->toDateTimeString(); // 2012-01-31 00:00:00 echo $dt->addCenturies(5); // 2512-01-31 00:00:00 echo $dt->addCentury(); // 2612-01-31 00:00:00 echo $dt->subCentury(); // 2512-01-31 00:00:00 echo $dt->subCenturies(5); // 2012-01-31 00:00:00 echo $dt->addYears(5); // 2017-01-31 00:00:00 echo $dt->addYear(); // 2018-01-31 00:00:00 echo $dt->subYear(); // 2017-01-31 00:00:00 echo $dt->subYears(5); // 2012-01-31 00:00:00 echo $dt->addQuarters(2); // 2012-07-31 00:00:00 echo $dt->addQuarter(); // 2012-10-31 00:00:00 echo $dt->subQuarter(); // 2012-07-31 00:00:00 echo $dt->subQuarters(2); // 2012-01-31 00:00:00 echo $dt->addMonths(60); // 2017-01-31 00:00:00 echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps echo $dt->subMonth(); // 2017-02-03 00:00:00 echo $dt->subMonths(60); // 2012-02-03 00:00:00 echo $dt->addDays(29); // 2012-03-03 00:00:00 echo $dt->addDay(); // 2012-03-04 00:00:00 echo $dt->subDay(); // 2012-03-03 00:00:00 echo $dt->subDays(29); // 2012-02-03 00:00:00 echo $dt->addWeekdays(4); // 2012-02-09 00:00:00 echo $dt->addWeekday(); // 2012-02-10 00:00:00 echo $dt->subWeekday(); // 2012-02-09 00:00:00 echo $dt->subWeekdays(4); // 2012-02-03 00:00:00 echo $dt->addWeeks(3); // 2012-02-24 00:00:00 echo $dt->addWeek(); // 2012-03-02 00:00:00 echo $dt->subWeek(); // 2012-02-24 00:00:00 echo $dt->subWeeks(3); // 2012-02-03 00:00:00 echo $dt->addHours(24); // 2012-02-04 00:00:00 echo $dt->addHour(); // 2012-02-04 01:00:00 echo $dt->subHour(); // 2012-02-04 00:00:00 echo $dt->subHours(24); // 2012-02-03 00:00:00 echo $dt->addMinutes(61); // 2012-02-03 01:01:00 echo $dt->addMinute(); // 2012-02-03 01:02:00 echo $dt->subMinute(); // 2012-02-03 01:01:00 echo $dt->subMinutes(61); // 2012-02-03 00:00:00 echo $dt->addSeconds(61); // 2012-02-03 00:01:01 echo $dt->addSecond(); // 2012-02-03 00:01:02 echo $dt->subSecond(); // 2012-02-03 00:01:01 echo $dt->subSeconds(61); // 2012-02-03 00:00:00 echo $dt->addMilliseconds(61); // 2012-02-03 00:00:00 echo $dt->addMillisecond(); // 2012-02-03 00:00:00 echo $dt->subMillisecond(); // 2012-02-03 00:00:00 echo $dt->subMillisecond(61); // 2012-02-03 00:00:00 echo $dt->addMicroseconds(61); // 2012-02-03 00:00:00 echo $dt->addMicrosecond(); // 2012-02-03 00:00:00 echo $dt->subMicrosecond(); // 2012-02-03 00:00:00 echo $dt->subMicroseconds(61); // 2012-02-03 00:00:00 // and so on for any unit: millenium, century, decade, year, quarter, month, week, day, weekday, // hour, minute, second, microsecond. // Generic methods add/sub (or subtract alias) can take many different arguments: echo $dt->add(61, 'seconds'); // 2012-02-03 00:01:01 echo $dt->sub('1 day'); // 2012-02-02 00:01:01 echo $dt->add(CarbonInterval::months(2)); // 2012-04-02 00:01:01 echo $dt->subtract(new DateInterval('PT1H')); // 2012-04-01 23:01:01 ``` For fun you can also pass negative values to `addXXX()`, in fact that's how `subXXX()` is implemented. P.S. Don't worry if you forget and use `addDay(5)` or `subYear(3)`, I have your back ;) By default, Carbon relies on the underlying parent class PHP DateTime behavior. As a result adding or subtracting months can overflow, example: ```php $dt = CarbonImmutable::create(2017, 1, 31, 0); echo $dt->addMonth(); // 2017-03-03 00:00:00 echo "\n"; echo $dt->subMonths(2); // 2016-12-01 00:00:00 ``` Since Carbon 2, you can set a local overflow behavior for each instance: ```php $dt = CarbonImmutable::create(2017, 1, 31, 0); $dt->settings([ 'monthOverflow' => false, ]); echo $dt->addMonth(); // 2017-02-28 00:00:00 echo "\n"; echo $dt->subMonths(2); // 2016-11-30 00:00:00 ``` This will apply for methods `addMonth(s)`, `subMonth(s)`, `add($x, 'month')`, `sub($x, 'month')` and equivalent quarter methods. But it won't apply for intervals objects or strings like `add(CarbonInterval::month())` or `add('1 month')`. Static helpers exist but are deprecated. If you're sure to need to apply global setting or work with version 1 of Carbon, [check the overflow static helpers section](https://carbon.nesbot.com/docs/#overflow-static-helpers) You also can use `->addMonthsNoOverflow`, `->subMonthsNoOverflow`, `->addMonthsWithOverflow` and `->subMonthsWithOverflow` (or the singular methods with no `s` to "month") to explicitly add/sub months with or without overflow no matter the current mode and the same for any bigger unit (quarter, year, decade, century, millennium). ```php $dt = Carbon::createMidnightDate(2017, 1, 31)->settings([ 'monthOverflow' => false, ]); echo $dt->copy()->addMonthWithOverflow(); // 2017-03-03 00:00:00 // plural addMonthsWithOverflow() method is also available echo $dt->copy()->subMonthsWithOverflow(2); // 2016-12-01 00:00:00 // singular subMonthWithOverflow() method is also available echo $dt->copy()->addMonthNoOverflow(); // 2017-02-28 00:00:00 // plural addMonthsNoOverflow() method is also available echo $dt->copy()->subMonthsNoOverflow(2); // 2016-11-30 00:00:00 // singular subMonthNoOverflow() method is also available echo $dt->copy()->addMonth(); // 2017-02-28 00:00:00 echo $dt->copy()->subMonths(2); // 2016-11-30 00:00:00 $dt = Carbon::createMidnightDate(2017, 1, 31)->settings([ 'monthOverflow' => true, ]); echo $dt->copy()->addMonthWithOverflow(); // 2017-03-03 00:00:00 echo $dt->copy()->subMonthsWithOverflow(2); // 2016-12-01 00:00:00 echo $dt->copy()->addMonthNoOverflow(); // 2017-02-28 00:00:00 echo $dt->copy()->subMonthsNoOverflow(2); // 2016-11-30 00:00:00 echo $dt->copy()->addMonth(); // 2017-03-03 00:00:00 echo $dt->copy()->subMonths(2); // 2016-12-01 00:00:00 ``` The same is available for years. You also can control overflow for any unit when working with unknown inputs: ```php $dt = CarbonImmutable::create(2018, 8, 30, 12, 00, 00); // Add hours without overflowing day echo $dt->addUnitNoOverflow('hour', 7, 'day'); // 2018-08-30 19:00:00 echo "\n"; echo $dt->addUnitNoOverflow('hour', 14, 'day'); // 2018-08-30 23:59:59 echo "\n"; echo $dt->addUnitNoOverflow('hour', 48, 'day'); // 2018-08-30 23:59:59 echo "\n-------\n"; // Substract hours without overflowing day echo $dt->subUnitNoOverflow('hour', 7, 'day'); // 2018-08-30 05:00:00 echo "\n"; echo $dt->subUnitNoOverflow('hour', 14, 'day'); // 2018-08-30 00:00:00 echo "\n"; echo $dt->subUnitNoOverflow('hour', 48, 'day'); // 2018-08-30 00:00:00 echo "\n-------\n"; // Set hours without overflowing day echo $dt->setUnitNoOverflow('hour', -7, 'day'); // 2018-08-30 00:00:00 echo "\n"; echo $dt->setUnitNoOverflow('hour', 14, 'day'); // 2018-08-30 14:00:00 echo "\n"; echo $dt->setUnitNoOverflow('hour', 25, 'day'); // 2018-08-30 23:59:59 echo "\n-------\n"; // Adding hours without overflowing month echo $dt->addUnitNoOverflow('hour', 7, 'month'); // 2018-08-30 19:00:00 echo "\n"; echo $dt->addUnitNoOverflow('hour', 14, 'month'); // 2018-08-31 02:00:00 echo "\n"; echo $dt->addUnitNoOverflow('hour', 48, 'month'); // 2018-08-31 23:59:59 ``` Any modifiable unit can be passed as argument of those methods: ```php $units = []; foreach (['millennium', 'century', 'decade', 'year', 'quarter', 'month', 'week', 'weekday', 'day', 'hour', 'minute', 'second', 'millisecond', 'microsecond'] as $unit) { $units[$unit] = Carbon::isModifiableUnit($unit); } echo json_encode($units, JSON_PRETTY_PRINT); /* { "millennium": true, "century": true, "decade": true, "year": true, "quarter": true, "month": true, "week": true, "weekday": true, "day": true, "hour": true, "minute": true, "second": true, "millisecond": true, "microsecond": true } */ ``` # Difference As `Carbon` extends `DateTime` it inherit its methods such as `diff()` that take a second date object as argument and returns a `DateInterval` instance. We also provide `diffAsCarbonInterval()` act like `diff()` but returns a `CarbonInterval` instance. Check [CarbonInterval chapter](https://carbon.nesbot.com/docs/#api-interval) for more information. Carbon add diff methods for each unit too such as `diffInYears()`, `diffInMonths()` and so on. `diffAsCarbonInterval()` and `diffIn*()` and `floatDiffIn*()` methods all can take 2 optional arguments: date to compare with (if missing, now is used instead), and an absolute boolean option (`true` by default) that make the method return an absolute value no matter which date is greater than the other. If set to `false`, it returns negative value when the instance the method is called on is greater than the compared date (first argument or now). Note that `diff()` prototype is different: its first argument (the date) is mandatory and its second argument (the absolute option) defaults to `false`. 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 122 seconds would be returned as 2 minutes and 2 seconds via a `DateInterval` instance. The `diffInMinutes()` function would simply return 2 while `diffInSeconds()` would return 122. 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. ```php echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0 $dtOttawa = Carbon::createMidnightDate(2000, 1, 1, 'America/Toronto'); $dtVancouver = Carbon::createMidnightDate(2000, 1, 1, 'America/Vancouver'); echo $dtOttawa->diffInHours($dtVancouver); // 3 echo $dtVancouver->diffInHours($dtOttawa); // 3 echo $dtOttawa->diffInHours($dtVancouver, false); // 3 echo $dtVancouver->diffInHours($dtOttawa, false); // -3 $dt = Carbon::createMidnightDate(2012, 1, 31); echo $dt->diffInDays($dt->copy()->addMonth()); // 31 echo $dt->diffInDays($dt->copy()->subMonth(), false); // -31 $dt = Carbon::createMidnightDate(2012, 4, 30); echo $dt->diffInDays($dt->copy()->addMonth()); // 30 echo $dt->diffInDays($dt->copy()->addWeek()); // 7 $dt = Carbon::createMidnightDate(2012, 1, 1); echo $dt->diffInMinutes($dt->copy()->addSeconds(59)); // 0 echo $dt->diffInMinutes($dt->copy()->addSeconds(60)); // 1 echo $dt->diffInMinutes($dt->copy()->addSeconds(119)); // 1 echo $dt->diffInMinutes($dt->copy()->addSeconds(120)); // 2 echo $dt->addSeconds(120)->secondsSinceMidnight(); // 120 $interval = $dt->diffAsCarbonInterval($dt->copy()->subYears(3), false); // diffAsCarbonInterval use same arguments as diff($other, $absolute) // (native method from \DateTime) // except $absolute is true by default for diffAsCarbonInterval and false for diff // $absolute parameter allow to get signed value if false, or always positive if true echo ($interval->invert ? 'minus ' : 'plus ') . $interval->years; // minus 3 ``` These methods have int-truncated results. That means `diffInMinutes` returns 1 for any difference between 1 included and 2 excluded. But same methods are available for float results: ```php echo Carbon::parse('06:01:23.252987')->floatDiffInSeconds('06:02:34.321450'); // 71.068463 echo Carbon::parse('06:01:23')->floatDiffInMinutes('06:02:34'); // 1.1833333333333 echo Carbon::parse('06:01:23')->floatDiffInHours('06:02:34'); // 0.019722222222222 // Those methods are absolute by default but can return negative value // setting the second argument to false when start date is greater than end date echo Carbon::parse('12:01:23')->floatDiffInHours('06:02:34', false); // -5.9802777777778 echo Carbon::parse('2000-01-01 12:00')->floatDiffInDays('2000-02-11 06:00'); // 40.75 echo Carbon::parse('2000-01-01')->floatDiffInWeeks('2000-02-11'); // 5.8571428571429 echo Carbon::parse('2000-01-15')->floatDiffInMonths('2000-02-24'); // 1.3103448275862 // floatDiffInMonths count as many full months as possible from the start date // (for instance 31 days if the start is in January), then consider the number // of days in the months for ending chunks to reach the end date. // So the following result (ending with 24 march is different from previous one with 24 february): echo Carbon::parse('2000-02-15 12:00')->floatDiffInMonths('2000-03-24 06:00'); // 1.2822580645161 // floatDiffInYears apply the same logic (and so different results with leap years) echo Carbon::parse('2000-02-15 12:00')->floatDiffInYears('2010-03-24 06:00'); // 10.100684931507 ``` ⚠️ Important note about the daylight saving times (DST), by default PHP DateTime does not take DST into account, that means for example that a day with only 23 hours like March the 30th 2014 in London will be counted as 24 hours long. ```php $date = new DateTime('2014-03-30 00:00:00', new DateTimeZone('Europe/London')); // DST off echo $date->modify('+25 hours')->format('H:i'); // 01:00 (DST on, 24 hours only have been actually added) ``` Carbon follow this behavior too for add/sub/diff seconds/minutes/hours. But we provide methods to works with *real* hours using timestamp: ```php $date = new Carbon('2014-03-30 00:00:00', 'Europe/London'); // DST off echo $date->addRealHours(25)->format('H:i'); // 02:00 (DST on) echo $date->diffInRealHours('2014-03-30 00:00:00'); // 25 echo $date->diffInHours('2014-03-30 00:00:00'); // 26 echo $date->diffInRealMinutes('2014-03-30 00:00:00'); // 1500 echo $date->diffInMinutes('2014-03-30 00:00:00'); // 1560 echo $date->diffInRealSeconds('2014-03-30 00:00:00'); // 90000 echo $date->diffInSeconds('2014-03-30 00:00:00'); // 93600 echo $date->diffInRealMilliseconds('2014-03-30 00:00:00'); // 90000000 echo $date->diffInMilliseconds('2014-03-30 00:00:00'); // 93600000 echo $date->diffInRealMicroseconds('2014-03-30 00:00:00'); // 90000000000 echo $date->diffInMicroseconds('2014-03-30 00:00:00'); // 93600000000 echo $date->subRealHours(25)->format('H:i'); // 00:00 (DST off) // with float diff: $date = new Carbon('2019-10-27 00:00:00', 'Europe/Paris'); echo $date->floatDiffInRealHours('2019-10-28 12:30:00'); // 37.5 echo $date->floatDiffInHours('2019-10-28 12:30:00'); // 36.5 echo $date->floatDiffInRealMinutes('2019-10-28 12:00:30'); // 2220.5 echo $date->floatDiffInMinutes('2019-10-28 12:00:30'); // 2160.5 echo $date->floatDiffInRealSeconds('2019-10-28 12:00:00.5'); // 133200.5 echo $date->floatDiffInSeconds('2019-10-28 12:00:00.5'); // 129600.5 // above day unit, "real" will affect the decimal part based on hours and smaller units echo $date->floatDiffInRealDays('2019-10-28 12:30:00'); // 1.5625 echo $date->floatDiffInDays('2019-10-28 12:30:00'); // 1.5208333333333 echo $date->floatDiffInRealWeeks('2019-10-28 12:30:00'); // 0.22321428571429 echo $date->floatDiffInWeeks('2019-10-28 12:30:00'); // 0.2172619047619 echo $date->floatDiffInRealMonths('2019-10-28 12:30:00'); // 0.050403225806452 echo $date->floatDiffInMonths('2019-10-28 12:30:00'); // 0.049059139784946 echo $date->floatDiffInRealYears('2019-10-28 12:30:00'); // 0.0042808219178082 echo $date->floatDiffInYears('2019-10-28 12:30:00'); // 0.0041666666666667 ``` The same way you can use `addRealX()` and `subRealX()` on any unit. There are also special filter functions `diffInDaysFiltered()`, `diffInHoursFiltered()` and `diffFiltered()`, to help you filter the difference by days, hours or a custom interval. For example to count the weekend days between two instances: ```php $dt = Carbon::create(2014, 1, 1); $dt2 = Carbon::create(2014, 12, 31); $daysForExtraCoding = $dt->diffInDaysFiltered(function(Carbon $date) { return $date->isWeekend(); }, $dt2); echo $daysForExtraCoding; // 104 $dt = Carbon::create(2014, 1, 1)->endOfDay(); $dt2 = $dt->copy()->startOfDay(); $littleHandRotations = $dt->diffFiltered(CarbonInterval::minute(), function(Carbon $date) { return $date->minute === 0; }, $dt2, true); // true as last parameter returns absolute value echo $littleHandRotations; // 24 $date = Carbon::now()->addSeconds(3666); echo $date->diffInSeconds(); // 3665 echo $date->diffInMinutes(); // 61 echo $date->diffInHours(); // 1 echo $date->diffInDays(); // 0 $date = Carbon::create(2016, 1, 5, 22, 40, 32); echo $date->secondsSinceMidnight(); // 81632 echo $date->secondsUntilEndOfDay(); // 4767 $date1 = Carbon::createMidnightDate(2016, 1, 5); $date2 = Carbon::createMidnightDate(2017, 3, 15); echo $date1->diffInDays($date2); // 435 echo $date1->diffInWeekdays($date2); // 311 echo $date1->diffInWeekendDays($date2); // 124 echo $date1->diffInWeeks($date2); // 62 echo $date1->diffInMonths($date2); // 14 echo $date1->diffInQuarters($date2); // 4 echo $date1->diffInYears($date2); // 1 ``` All diffIn*Filtered method take 1 callable filter as required parameter and a date object as optional second parameter, if missing, now is used. You may also pass true as third parameter to get absolute values. For advanced handle of the week/weekend days, use the following tools: ```php echo implode(', ', Carbon::getDays()); // Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday $saturday = new Carbon('first saturday of 2019'); $sunday = new Carbon('first sunday of 2019'); $monday = new Carbon('first monday of 2019'); echo implode(', ', Carbon::getWeekendDays()); // 6, 0 var_dump($saturday->isWeekend()); // bool(true) var_dump($sunday->isWeekend()); // bool(true) var_dump($monday->isWeekend()); // bool(false) Carbon::setWeekendDays([ Carbon::SUNDAY, Carbon::MONDAY, ]); echo implode(', ', Carbon::getWeekendDays()); // 0, 1 var_dump($saturday->isWeekend()); // bool(false) var_dump($sunday->isWeekend()); // bool(true) var_dump($monday->isWeekend()); // bool(true) Carbon::setWeekendDays([ Carbon::SATURDAY, Carbon::SUNDAY, ]); // weekend days and start/end of week or not linked Carbon::setWeekStartsAt(Carbon::FRIDAY); Carbon::setWeekEndsAt(Carbon::WEDNESDAY); // and it does not need neither to precede the start var_dump(Carbon::getWeekStartsAt() === Carbon::FRIDAY); // bool(true) var_dump(Carbon::getWeekEndsAt() === Carbon::WEDNESDAY); // bool(true) echo $saturday->copy()->startOfWeek()->toRfc850String(); // Friday, 31-Mar-23 00:00:00 UTC echo $saturday->copy()->endOfWeek()->toRfc850String(); // Wednesday, 05-Apr-23 23:59:59 UTC // Be very careful with those global setters, remember, some other // code or third-party library in your app may expect initial weekend // days to work properly. Carbon::setWeekStartsAt(Carbon::MONDAY); Carbon::setWeekEndsAt(Carbon::SUNDAY); echo $saturday->copy()->startOfWeek()->toRfc850String(); // Monday, 27-Mar-23 00:00:00 UTC echo $saturday->copy()->endOfWeek()->toRfc850String(); // Sunday, 02-Apr-23 23:59:59 UTC ``` # Difference for Humans 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. The lone argument for the function is the other Carbon instance to diff against, and of course it defaults to `now()` if not specified. This method will add a phrase after the difference value relative to the instance and the passed in instance. There are 4 possibilities: - When comparing a value in the past to default now: - 1 hour ago - 5 months ago - When comparing a value in the future to default now: - 1 hour from now - 5 months from now - When comparing a value in the past to another value: - 1 hour before - 5 months before - When comparing a value in the future to another value: - 1 hour after - 5 months after You may also pass `CarbonInterface::DIFF_ABSOLUTE` as a 2nd parameter to remove the modifiers *ago*, *from now*, etc : `diffForHumans($other, CarbonInterface::DIFF_ABSOLUTE)`, `CarbonInterface::DIFF_RELATIVE_TO_NOW` to get modifiers *ago* or *from now*, `CarbonInterface::DIFF_RELATIVE_TO_OTHER` to get the modifiers *before* or *after* or `CarbonInterface::DIFF_RELATIVE_AUTO` (default mode) to get the modifiers either *ago*/*from now* if the 2 second argument is null or *before*/*after* if not. You may pass `true` as a 3rd parameter to use short syntax if available in the locale used : `diffForHumans($other, CarbonInterface::DIFF_RELATIVE_AUTO, true)`. You may pass a number between 1 and 6 as a 4th parameter to get the difference in multiple parts (more precise diff) : `diffForHumans($other, CarbonInterface::DIFF_RELATIVE_AUTO, false, 4)`. The `$other` instance can be a DateTime, a Carbon instance or any object that implement DateTimeInterface, if a string is passed it will be parsed to get a Carbon instance and if `null` is passed, `Carbon::now()` will be used instead. To avoid having too much argument and mix the order, you can use the verbose methods: - `shortAbsoluteDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `longAbsoluteDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `shortRelativeDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `longRelativeDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `shortRelativeToNowDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `longRelativeToNowDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `shortRelativeToOtherDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` - `longRelativeToOtherDiffForHumans(DateTimeInterface | null $other = null, int $parts = 1)` PS: `$other` and `$parts` arguments can be swapped as need. ```php // The most typical usage is for comments // The instance is the date the comment was created and its being compared to default now() echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 11 months after $dt = Carbon::createFromDate(2011, 8, 1); echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after echo Carbon::now()->addSeconds(5)->diffForHumans(); // 4 seconds from now echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago echo Carbon::now()->subDays(24)->longAbsoluteDiffForHumans(); // 3 weeks echo Carbon::parse('2019-08-03')->diffForHumans('2019-08-13'); // 1 week before echo Carbon::parse('2000-01-01 00:50:32')->diffForHumans('@946684800'); // 50 minutes after echo Carbon::create(2018, 2, 26, 4, 29, 43)->longRelativeDiffForHumans(Carbon::create(2016, 6, 21, 0, 0, 0), 6); // 1 year 8 months 5 days 4 hours 29 minutes 43 seconds after ``` You can also change the locale of the string using `$date->locale('fr')` before the diffForHumans() call. See the [localization](https://carbon.nesbot.com/docs/#api-localization) section for more detail. Since 2.9.0 diffForHumans() parameters can be passed as an array: If the argument is omitted or set to `null`, only `Carbon::NO_ZERO_DIFF` is enabled. Available options are: - `Carbon::ROUND`/`Carbon::CEIL`/`Carbon::FLOOR` (none by default): only one of the 3 can be used at a time (other are ignored) and it requires`'parts'`to be set. By default, once the diff has as parts as`'parts'` setting requested and omit all remaining units. - If `Carbon::ROUND` is enabled, the remaining units are summed and if they are **>= 0.5** of the last unit of the diff, this unit will be rounded to the upper value. - If `Carbon::CEIL` is enabled, the remaining units are summed and if they are **> 0** of the last unit of the diff, this unit will be rounded to the upper value. - If `Carbon::FLOOR` is enabled, the last diff unit is rounded down. It makes no difference from the default behavior for `diffForHumans()` as the interval can't have overflow, but this option may be needed when used with `CarbonInterval::forHumans()` (and unchecked intervals that may have 60 minutes or more, 24 hours or more, etc.). For example: `CarbonInterval::make('1 hour and 67 minutes')->forHumans(['parts' => 1])` returns `"1 hour"` while `CarbonInterval::make('1 hour and 67 minutes')->forHumans(['parts' => 1, 'options' => Carbon::FLOOR])` returns `"2 hours"`. - `Carbon::NO_ZERO_DIFF` (enabled by default): turns empty diff into 1 second - `Carbon::JUST_NOW` disabled by default): turns diff from now to now into "just now" - `Carbon::ONE_DAY_WORDS` (disabled by default): turns "1 day from now/ago" to "yesterday/tomorrow" - `Carbon::TWO_DAY_WORDS` (disabled by default): turns "2 days from now/ago" to "before yesterday/after - `Carbon::SEQUENTIAL_PARTS_ONLY` (disabled by default): will keep only the first sequence of units of the interval, for example if the diff would have been "2 weeks 1 day 34 minutes 12 seconds" as day and minute are not consecutive units, you will get: "2 weeks 1 day". Use the pipe operator to enable/disable multiple option at once, example: `Carbon::ONE_DAY_WORDS | Carbon::TWO_DAY_WORDS` You also can use `Carbon::enableHumanDiffOption($option)`, `Carbon::disableHumanDiffOption($option)`, `Carbon::setHumanDiffOptions($options)` to change the default options and `Carbon::getHumanDiffOptions()` to get default options but you should avoid using it as being static it may conflict with calls from other code parts/third-party libraries. Aliases and reverse methods are provided for semantic purpose: - `from($other = null, $syntax = null, $short = false, $parts = 1, $options = null)` (alias of diffForHumans) - `since($other = null, $syntax = null, $short = false, $parts = 1, $options = null)` (alias of diffForHumans) - `to($other = null, $syntax = null, $short = false, $parts = 1, $options = null)` (inverse result, swap before and future diff) - `until($other = null, $syntax = null, $short = false, $parts = 1, $options = null)` (alias of to) - `fromNow($syntax = null, $short = false, $parts = 1, $options = null)` (alias of from with first argument omitted unless the first argument is a `DateTimeInterface`, now used instead), for semantic usage: produce an "3 hours from now"-like string with dates in the future - `ago($syntax = null, $short = false, $parts = 1, $options = null)` (alias of fromNow), for semantic usage: produce an "3 hours ago"-like string with dates in the past - `toNow($syntax = null, $short = false, $parts = 1, $options = null)` (alias of to with first argument omitted, now used instead) - `timespan($other = null, $timezone = null)` calls diffForHumans with options `join = ', '` (coma-separated), `syntax = CarbonInterface::DIFF_ABSOLUTE` (no "ago"/"from now"/"before"/"after" wording), `options = CarbonInterface::NO_ZERO_DIFF` (no "just now"/"yesterday"/"tomorrow" wording), `parts = -1` (no limits) In this mode, you can't change options but you can pass an optional date to compare with or a string + timezone to parse to get this date. # Modifiers 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 for unit bigger than days. The only one slightly different is the `average()` function. It moves your instance to the middle date between itself and the provided Carbon argument. The powerful native [`modify()` method of `DateTime`](https://www.php.net/manual/en/datetime.modify.php) is available untouched. But we also provide an enhanced version of it: `change()` that allows some additional syntax like `"next 3pm"` and that is called internally by `->next()` and `->previous()`. ```php $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->startOfSecond()->format('s.u'); // 45.000000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->endOfSecond()->format('s.u'); // 45.999999 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->startOf('second')->format('s.u'); // 45.000000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->endOf('second')->format('s.u'); // 45.999999 // ->startOf() and ->endOf() are dynamic equivalents to those methods $dt = Carbon::create(2012, 1, 31, 15, 32, 45); echo $dt->startOfMinute(); // 2012-01-31 15:32:00 $dt = Carbon::create(2012, 1, 31, 15, 32, 45); echo $dt->endOfMinute(); // 2012-01-31 15:32:59 $dt = Carbon::create(2012, 1, 31, 15, 32, 45); echo $dt->startOfHour(); // 2012-01-31 15:00:00 $dt = Carbon::create(2012, 1, 31, 15, 32, 45); echo $dt->endOfHour(); // 2012-01-31 15:59:59 $dt = Carbon::create(2012, 1, 31, 15, 32, 45); echo Carbon::getMidDayAt(); // 12 echo $dt->midDay(); // 2012-01-31 12:00:00 Carbon::setMidDayAt(13); echo Carbon::getMidDayAt(); // 13 echo $dt->midDay(); // 2012-01-31 13:00:00 Carbon::setMidDayAt(12); $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfDay(); // 2012-01-31 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfDay(); // 2012-01-31 23:59:59 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfMonth(); // 2012-01-01 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfMonth(); // 2012-01-31 23:59:59 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfYear(); // 2012-01-01 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfYear(); // 2012-12-31 23:59:59 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfDecade(); // 2010-01-01 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfDecade(); // 2019-12-31 23:59:59 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfCentury(); // 2001-01-01 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfCentury(); // 2100-12-31 23:59:59 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->startOfWeek(); // 2012-01-30 00:00:00 var_dump($dt->dayOfWeek == Carbon::MONDAY); // bool(true) : ISO8601 week starts on Monday $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->endOfWeek(); // 2012-02-05 23:59:59 var_dump($dt->dayOfWeek == Carbon::SUNDAY); // bool(true) : ISO8601 week ends on Sunday $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->next(Carbon::WEDNESDAY); // 2012-02-01 00:00:00 var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true) echo $dt->next('Wednesday'); // 2012-02-08 00:00:00 echo $dt->next('04:00'); // 2012-02-08 04:00:00 echo $dt->next('12:00'); // 2012-02-08 12:00:00 echo $dt->next('04:00'); // 2012-02-09 04:00:00 $dt = Carbon::create(2012, 1, 1, 12, 0, 0); echo $dt->next(); // 2012-01-08 00:00:00 $dt = Carbon::create(2012, 1, 31, 12, 0, 0); echo $dt->previous(Carbon::WEDNESDAY); // 2012-01-25 00:00:00 var_dump($dt->dayOfWeek == Carbon::WEDNESDAY); // bool(true) $dt = Carbon::create(2012, 1, 1, 12, 0, 0); echo $dt->previous(); // 2011-12-25 00:00:00 $start = Carbon::create(2014, 1, 1, 0, 0, 0); $end = Carbon::create(2014, 1, 30, 0, 0, 0); echo $start->average($end); // 2014-01-15 12:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfMonth(); // 2014-05-01 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfMonth(Carbon::MONDAY); // 2014-05-05 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfMonth(); // 2014-05-31 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfMonth(Carbon::TUESDAY); // 2014-05-27 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->nthOfMonth(2, Carbon::SATURDAY); // 2014-05-10 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfQuarter(); // 2014-04-01 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfQuarter(Carbon::MONDAY); // 2014-04-07 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfQuarter(); // 2014-06-30 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfQuarter(Carbon::TUESDAY); // 2014-06-24 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->nthOfQuarter(2, Carbon::SATURDAY); // 2014-04-12 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->startOfQuarter(); // 2014-04-01 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->endOfQuarter(); // 2014-06-30 23:59:59 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfYear(); // 2014-01-01 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->firstOfYear(Carbon::MONDAY); // 2014-01-06 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfYear(); // 2014-12-31 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->lastOfYear(Carbon::TUESDAY); // 2014-12-30 00:00:00 echo Carbon::create(2014, 5, 30, 0, 0, 0)->nthOfYear(2, Carbon::SATURDAY); // 2014-01-11 00:00:00 echo Carbon::create(2018, 2, 23, 0, 0, 0)->nextWeekday(); // 2018-02-26 00:00:00 echo Carbon::create(2018, 2, 23, 0, 0, 0)->previousWeekday(); // 2018-02-22 00:00:00 echo Carbon::create(2018, 2, 21, 0, 0, 0)->nextWeekendDay(); // 2018-02-24 00:00:00 echo Carbon::create(2018, 2, 21, 0, 0, 0)->previousWeekendDay(); // 2018-02-18 00:00:00s ``` Rounding is also available for any unit: ```php $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->roundMillisecond()->format('H:i:s.u'); // 15:32:45.654000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->roundSecond()->format('H:i:s.u'); // 15:32:46.000000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->floorSecond()->format('H:i:s.u'); // 15:32:45.000000 $dt = new Carbon('2012-01-31 15:32:15'); echo $dt->roundMinute()->format('H:i:s'); // 15:32:00 $dt = new Carbon('2012-01-31 15:32:15'); echo $dt->ceilMinute()->format('H:i:s'); // 15:33:00 // and so on up to millennia! // precision rounding can be set, example: rounding to ten minutes $dt = new Carbon('2012-01-31 15:32:15'); echo $dt->roundMinute(10)->format('H:i:s'); // 15:30:00 // and round, floor and ceil methods are shortcut for second rounding: $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->round()->format('H:i:s.u'); // 15:32:46.000000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->floor()->format('H:i:s.u'); // 15:32:45.000000 $dt = new Carbon('2012-01-31 15:32:45.654321'); echo $dt->ceil()->format('H:i:s.u'); // 15:32:46.000000 // you can also pass the unit dynamically (and still precision as second argument): $dt = new Carbon('2012-01-31'); echo $dt->roundUnit('month', 2)->format('Y-m-d'); // 2012-01-01 $dt = new Carbon('2012-01-31'); echo $dt->floorUnit('month')->format('Y-m-d'); // 2012-01-01 $dt = new Carbon('2012-01-31'); echo $dt->ceilUnit('month', 4)->format('Y-m-d'); // 2012-05-01 ``` # Constants The following constants are defined in the Carbon class. ```php // These getters specifically return integers, ie intval() var_dump(Carbon::SUNDAY); // int(0) var_dump(Carbon::MONDAY); // int(1) var_dump(Carbon::TUESDAY); // int(2) var_dump(Carbon::WEDNESDAY); // int(3) var_dump(Carbon::THURSDAY); // int(4) var_dump(Carbon::FRIDAY); // int(5) var_dump(Carbon::SATURDAY); // int(6) var_dump(Carbon::YEARS_PER_CENTURY); // int(100) var_dump(Carbon::YEARS_PER_DECADE); // int(10) var_dump(Carbon::MONTHS_PER_YEAR); // int(12) var_dump(Carbon::WEEKS_PER_YEAR); // int(52) var_dump(Carbon::DAYS_PER_WEEK); // int(7) var_dump(Carbon::HOURS_PER_DAY); // int(24) var_dump(Carbon::MINUTES_PER_HOUR); // int(60) var_dump(Carbon::SECONDS_PER_MINUTE); // int(60) ``` ```php $dt = Carbon::createFromDate(2012, 10, 6); if ($dt->dayOfWeek === Carbon::SATURDAY) { echo 'Place bets on Ottawa Senators Winning!'; } ``` # Serialization The Carbon instances can be serialized (including CarbonImmutable, CarbonInterval and CarbonPeriod). ```php $dt = Carbon::create(2012, 12, 25, 20, 30, 00, 'Europe/Moscow'); echo serialize($dt); // O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"2012-12-25 20:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";} // same as: echo $dt->serialize(); // O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"2012-12-25 20:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";} $dt = 'O:13:"Carbon\Carbon":3:{s:4:"date";s:26:"2012-12-25 20:30:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:13:"Europe/Moscow";}'; echo unserialize($dt)->format('Y-m-d\TH:i:s.uP T'); // 2012-12-25T20:30:00.000000+04:00 MSK // same as: echo Carbon::fromSerialized($dt)->format('Y-m-d\TH:i:s.uP T'); // 2012-12-25T20:30:00.000000+04:00 MSK ``` # JSON The Carbon instances can be encoded to and decoded from JSON. Since the version 2.4, we explicitly require the PHP JSON extension. It should have no impact as this extension is bundled by default with PHP. If the extension is disabled, be aware you will be locked on 2.3. But you still can use `--ignore-platform-reqs` on composer update/install to upgrade then polyfill the missing `JsonSerializable` interface by including [JsonSerializable.php](https://github.com/briannesbitt/Carbon/blob/2.3.0/src/JsonSerializable.php). ```php $dt = Carbon::create(2012, 12, 25, 20, 30, 00, 'Europe/Moscow'); echo json_encode($dt); // "2012-12-25T16:30:00.000000Z" $json = '{"date":"2012-12-25 20:30:00.000000","timezone_type":3,"timezone":"Europe\/Moscow"}'; $dt = Carbon::__set_state(json_decode($json, true)); echo $dt->format('Y-m-d\TH:i:s.uP T'); // 2012-12-25T20:30:00.000000+04:00 MSK ``` You can use `settings(['toJsonFormat' => $format])` to customize the serialization. ```php $dt = Carbon::create(2012, 12, 25, 20, 30, 00, 'Europe/Moscow')->settings([ 'toJsonFormat' => function ($date) { return $date->getTimestamp(); }, ]); echo json_encode($dt); // 1356453000 ``` If you want to apply this globally, first consider using factory, else or if you use Carbon 1 you can use: ```php $dt = Carbon::create(2012, 12, 25, 20, 30, 00, 'Europe/Moscow'); Carbon::serializeUsing(function ($date) { return $date->valueOf(); }); echo json_encode($dt); // 1356453000000 // Call serializeUsing with null to reset the serializer: Carbon::serializeUsing(null); ``` 未完待续... Last modification:May 17, 2023 © Allow specification reprint Like 如果觉得我的文章对你有用,请随意赞赏