Lấy php năm hiện tại

One important thing you should remember is that the timestamp value returned by time() is time-zone agnostic and gets the number of seconds since 1 January 1970 at 00:00:00 UTC. This means that at a particular point in time, this function will return the same value in the US, Europe, India, Japan, ...

date() will format a time-zone agnostic timestamp according to the default timezone set with date_default_timezone_set(...). Local time. If you want to output as UTC time use:

function dateUTC($format, $timestamp = null)
{
    if ($timestamp === null) $timestamp = time();

    $tz = date_default_timezone_get();
    date_default_timezone_set('UTC');

    $result = date($format, $timestamp);

    date_default_timezone_set($tz);
    return $result;
}
/>

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

use Carbon\Carbon;
69 method

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

use Carbon\Carbon;
70 or to
use Carbon\Carbon;
71 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
use Carbon\Carbon;
72

$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

use Carbon\Carbon;
68, a few other static instantiation helpers exist to create widely known instances. The only thing to really notice here is that
use Carbon\Carbon;
74,
use Carbon\Carbon;
75 and
use Carbon\Carbon;
76, besides behaving as expected, all accept a timezone parameter and each has their time value set to
use Carbon\Carbon;
77

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00

The next group of static helpers are the

use Carbon\Carbon;
78 helpers. Most of the static
use Carbon\Carbon;
79 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
use Carbon\Carbon;
80 with an informative message. The message is obtained from an DateTime. getLastErrors() call

$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";

use Carbon\Carbon;
81 will default the time to now.
use Carbon\Carbon;
82 will default the date to today.
use Carbon\Carbon;
83 will default any null parameter to the current respective value. As before, the
use Carbon\Carbon;
84 defaults to the current timezone and otherwise can be a DateTimeZone instance or simply a string timezone value. The only special case is for
use Carbon\Carbon;
83 that has minimum value as default for missing argument but default on current value when you pass explicitly
use Carbon\Carbon;
86

use Carbon\Carbon;
0

Create exceptions occurs on such negative values but not on overflow, to get exceptions on overflow, use

use Carbon\Carbon;
87

use Carbon\Carbon;
1

Note 1. 2018-02-29 also throws an exception while 2020-02-29 does not since 2020 is a leap year

Note 2.

use Carbon\Carbon;
88 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

use Carbon\Carbon;
89 between
use Carbon\Carbon;
90 and
use Carbon\Carbon;
91 even if it doesn't regarding to Gregorian calendar. That's why years lower than 1 will throw an exception using
use Carbon\Carbon;
92. Check for year-0 detection

use Carbon\Carbon;
2

use Carbon\Carbon;
93 is mostly a wrapper for the base php function DateTime. createFromFormat. The difference being again the
use Carbon\Carbon;
84 argument can be a DateTimeZone instance or a string timezone value. Also, if there are errors with the format this function will call the
use Carbon\Carbon;
95 method and then throw a
use Carbon\Carbon;
80 with the errors as the message

use Carbon\Carbon;
3

You can test if a date matches a format for

use Carbon\Carbon;
93 (e. g. date/time components, modifiers or separators) using
use Carbon\Carbon;
98 or
use Carbon\Carbon;
99 which also ensure data is actually enough to create an instance

use Carbon\Carbon;
4

You can create instances from unix timestamps.

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
00 create a Carbon instance equal to the given timestamp and will set the timezone as well or default it to the current timezone.
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
01, is different in that the timezone will remain UTC (GMT+00. 00), it's equivalent to
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
02 but it supports int, float or string containing one or more numbers (like the one produced by
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
03) so it can also set microseconds with no precision lost. The third,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
04, accepts a timestamp in milliseconds instead of seconds. Negative timestamps are also allowed

use Carbon\Carbon;
5

You can also create a

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
05 of an existing Carbon instance. As expected the date, time and timezone values are all copied to the new instance

use Carbon\Carbon;
6

You can use

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
06 on an existing Carbon instance to get a new instance at now in the same timezone

use Carbon\Carbon;
7

Cuối cùng, nếu bạn thấy mình kế thừa một phiên bản

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
07 từ một thư viện khác, đừng lo. Bạn có thể tạo một phiên bản
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
08 thông qua một phương pháp thân thiện với
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
09. Hoặc sử dụng phương pháp thậm chí linh hoạt hơn
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
10 có thể trả về một phiên bản Carbon mới từ DateTime, Carbon hoặc từ một chuỗi, nếu không, nó chỉ trả về null

use Carbon\Carbon;
8

Carbon 2 (yêu cầu PHP >= 7. 1) hỗ trợ hoàn hảo micro giây. Nhưng nếu bạn sử dụng Carbon 1 và PHP <7. 1, đọc của chúng tôi

Trước PHP 7. 1 micro giây DateTime không được thêm vào phiên bản

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
11 và không thể thay đổi sau đó, điều này có nghĩa là

use Carbon\Carbon;
9

To work around this limitation in Carbon, we append microseconds when calling

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
12 in PHP < 7. 1, but this feature can be disabled on demand (no effect in PHP >= 7. 1)

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
0

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

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
1

Min and max value mainly depends on the system (32 or 64 bit)

With a 32-bit OS system or 32-bit version of PHP (you can check it in PHP with

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
13), the minimum value is the 0-unix-timestamp (1970-01-01 00. 00. 00) and the maximum is the timestamp given by the constant
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
14

With a 64-bit OS system and 64-bit version of PHP, the minimum is 01-01-01 00. 00. 00 and maximum is 9999-12-31 23. 59. 59. It's even possible to use negative year up to -9999 but be aware you may not have accurate results with some operations as the year 0 exists in PHP but not in the Gregorian calendar

With Carbon 2, localization changed a lot, 750 new locales are supported and we now embed locale formats, day names, month names, ordinal suffixes, meridiem, week start and more. While Carbon 1 provided partial support and relied on third-party like IntlDateFormatter class and language packages for advanced translation, you now benefit of a wide internationalization support. You still use Carbon 1? I hope you would consider to upgrade, version 2 has really cool new features. Otherwise you can find the

Unfortunately the base class DateTime does not have any localization support. To begin localization support a

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
15 method was added. The implementation makes a call to strftime using the current instance timestamp. If you first set the current locale with PHP function setlocale() then the string returned will be formatted in the correct locale

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
2

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
16 has also been localized. You can set the Carbon locale by using the static
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
17 function and get the current setting with
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
18

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
3

Or you can isolate some code with a given locale

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
4

Some languages require utf8 encoding to be printed (locale packages that does not ends with

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
19 mainly). In this case you can use the static method
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
20 to encode the result of the formatLocalized() call to the utf8 charset

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
5

on Linux
If you have trouble with translations, check locales installed in your system (local and production)

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
21 to list locales enabled
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
22 to install a new locale
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
23 to publish all locale enabled
And reboot your system

Since 2. 9. 0, you can easily customize translations

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
6

You can use fallback locales by passing in order multiple ones to

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
24

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
7

In the example above, it will try to find translations in "xx" in priority, then in "xy" if missing, then in "es", so here, you get "Xday" from "xx", "Yday" from "xy", and "hace" and "minutos" from "es"

Note that you can also use an other translator with

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
25 as long as the given translator implements
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
26. And you can get the global default translator using
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
27 (and
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
28 and
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
29 for the fallback locale, setFallbackLocale can be called multiple times to get multiple fallback locales) but as those method will change the behavior globally (including third-party libraries you may have in your app), it might cause unexpected results. You should rather customize translation using custom locales as in the example above

Carbon embed a default translator that extends Symfony\Component\Translation\Translator You can

So the support of a locale for

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
30, getters such as
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
31,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
32 and short variants is driven by locales installed in your operating system. For other translations, it's supported internally thanks to Carbon community. You can check what's supported with the following methods

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
8

So, here is the new recommended way to handle internationalization with Carbon

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
9

The

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
33 method only change the language for the current instance and has precedence over global settings. We recommend you this approach so you can't have conflict with other places or third-party libraries that could use Carbon. Nevertheless, to avoid calling
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
33 each time, you can use factories

$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.
0

You can call any static Carbon method on a factory (make, now, yesterday, tomorrow, parse, create, etc. ) Factory (and FactoryImmutable that generates CarbonImmutable instances) are the best way to keep things organized and isolated. As often as possible we recommend you to work with UTC dates, then apply locally (or with a factory) the timezone and the language before displaying dates to the user

What factory actually do is using the method name as static constructor then call

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
35 method which is a way to group in one call settings of locale, timezone, months/year overflow, etc. ()

$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.
1

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
35 also allow to pass local macros

$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.
2

Factory settings can be changed afterward with

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
37 or to merge new settings with existing ones
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
38 and the class to generate can be initialized as the second argument of the construct then changed later with
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
39

$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.
3

Previously there was

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
40 that set globally the locale. But as for our other static setters, we highly discourage you to use it. It breaks the principle of isolation because the configuration will apply for every class that uses Carbon

You also may know

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
41 method from Carbon 1. This method still works the same in Carbon 2 but you should better use
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
42 instead

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
43 use ISO format rather than PHP-specific format and use inner translations rather than language packages you need to install on every machine where you deploy your application.
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
44 method is compatible with momentjs format method, it means you can use same format strings as you may have used in moment from front-end or node. js. Here are some examples

$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.
4

You can also create date from ISO formatted strings

$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.
5

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
45 use contextualized methods for day names and month names as they can have multiple forms in some languages, see the following examples

$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.
6

Here is the complete list of available replacements (examples given with

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
46)

MãVí dụMô tảOD5Số ngày với các số thay thế, chẳng hạn như 三 cho 3 nếu ngôn ngữ là ja_JPOM1Số tháng với các số thay thế, chẳng hạn như ၀၂ cho 2 nếu ngôn ngữ là my_MMOY2017Số năm với các số thay thế, chẳng hạn như ۱۹۹۸ cho 1998 nếu ngôn ngữ là faOH1724, số giờ với các số thay thế, chẳng hạn như ႑႓ cho . 00Time zone offset HH. mmZZ+0000Time zone offset HHmm

Some macro-formats are also available. Here are examples of each in some languages

Codeenfrjahr
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
48
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
49
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
50

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
51

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
52

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
53

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
54

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
55

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
56

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
57

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
51
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
53
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
55
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
57

Khi bạn sử dụng định dạng macro với

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
62, bạn có thể chỉ định ngôn ngữ để chọn ngôn ngữ mà định dạng macro sẽ được tìm kiếm trong đó

$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.
7

An other usefull translated method is

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
63

$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.
8

If you know momentjs, then it works the same way. Bạn có thể chuyển ngày tham chiếu làm đối số thứ hai, ngày khác hiện được sử dụng. Và bạn có thể tùy chỉnh một hoặc nhiều định dạng bằng cách sử dụng đối số thứ hai (các định dạng để chuyển dưới dạng khóa mảng là. sameDay, next Day, next Week, last Day, last Week và sameElse)

$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.
9

để có cái nhìn tổng quan về 280 ngôn ngữ (và 823 biến thể khu vực) được hỗ trợ bởi phiên bản Carbon cuối cùng

aaAfar✅✅✅✅✅✅✅✅afAfrikaans✅✅❌✅✅✅✅❌agqAghem✅✅✅✅✅❌❌✅agrAguaruna✅✅✅✅✅✅✅✅akaAkan✅✅✅✅✅✅✅✅am Amharic✅✅✅✅

Nếu bạn có thể thêm các bản dịch bị thiếu hoặc ngôn ngữ bị thiếu, vui lòng truy cập công cụ dịch, rất mong nhận được sự giúp đỡ của bạn

Lưu ý rằng nếu bạn sử dụng Laravel 5. 5+, ngôn ngữ sẽ được đặt tự động theo lần thực thi

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
64 cuối cùng hiện tại. Vì vậy,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
65,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
44,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
67 và các thuộc tính được bản địa hóa như
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
68 hoặc
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
69 sẽ được bản địa hóa một cách minh bạch

Theo mặc định, mỗi phiên bản Carbon, CarbonImmutable, CarbonInterval hoặc CarbonPeriod được liên kết với phiên bản

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
70 theo bộ ngôn ngữ của nó. Bạn có thể lấy và/hoặc thay đổi nó bằng cách sử dụng
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
71/
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
72

Nếu bạn thích mẫu

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
73, bạn có thể sử dụng
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
74 hoạt động giống như
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
75 nhưng dịch chuỗi bằng ngôn ngữ hiện tại

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
0

Được cảnh báo rằng một số chữ cái như

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
76 không được hỗ trợ vì chúng không thể dịch một cách an toàn và
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
67 cung cấp cú pháp ngắn hơn nhưng ít khả năng hơn
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
42

Bạn có thể tùy chỉnh hành vi của phương thức

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
75 để sử dụng bất kỳ phương thức nào khác hoặc phương thức tùy chỉnh thay vì phương thức gốc từ lớp DateTime của 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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
1

Bạn có thể dịch một chuỗi từ ngôn ngữ này sang ngôn ngữ khác bằng cách sử dụng bản dịch ngày có sẵn trong Carbon

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
2

Nếu ngôn ngữ đầu vào không được chỉ định, thay vào đó,

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
18 được sử dụng. Nếu ngôn ngữ đầu ra không được chỉ định, thì thay vào đó, hãy sử dụng
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
81. Bạn cũng có thể dịch bằng cách sử dụng ngôn ngữ của phiên bản với

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
3

Bạn có thể trực tiếp sử dụng các chuỗi trong bất kỳ ngôn ngữ nào để tạo một đối tượng ngày tháng với

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
82

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
4

Bạn cũng có thể sử dụng "hôm nay", "hôm nay lúc 8 giờ. 00", "hôm qua", "sau ngày mai", v.v. equivalents in the given language

Or with custom format using

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
83 (use the
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
73 pattern for replacements)

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
5

The equivalent method using ISO format is

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
85

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
6

To get some interesting info about languages (such as complete ISO name or native name, region (for example to be displayed in a languages selector), you can use

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
86

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
7

Please let me thank some projects that helped us a lot to support more locales, and internationalization features

  • jenssegers/date. many features were in this project that extends Carbon before being in Carbon itself
  • momentjs. many features are inspired by momentjs and made to be compatible with this front-side pair project
  • glibc was a strong base for adding and checking languages
  • svenfuchs/rails-i18n also helped to add and check languages
  • We used glosbe. com a lot to check translations and fill blanks

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 used when retrieving any relative time from Carbon (now, today, yesterday, next month, etc. )

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
8

A more meaning full example

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
9

Relative phrases are also mocked according to the given "now" instance

$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
0

Kể từ Carbon 2. 56. 0,

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
87 không còn ảnh hưởng đến múi giờ của phiên bản
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
88 mà bạn sẽ nhận được. Điều này được thực hiện bởi vì trong cuộc sống thực,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
88 trả về một ngày có múi giờ từ
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
90. Và các bài kiểm tra nên phản ánh điều này

Bạn có thể sử dụng

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
91 để mô phỏng thời gian và thay đổi múi giờ mặc định bằng cách sử dụng
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
92

$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
1

Danh sách các từ được coi là sửa đổi tương đối là

  • +
  • -
  • trước kia
  • đầu tiên
  • tiếp theo
  • Cuối cùng
  • cái này
  • hôm nay
  • ngày mai
  • hôm qua

Xin lưu ý rằng tương tự như các phương thức next(), previous() và modify(), một số công cụ sửa đổi tương đối này sẽ đặt thời gian thành 00. 00. 00

Cả

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
93 và
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
94 đều có thể lấy múi giờ làm đối số thứ hai

$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
2

Xem Carbonite để biết thêm các tính năng thử nghiệm Carbon nâng cao

Carbonite là một gói bổ sung mà bạn có thể dễ dàng cài đặt bằng trình soạn nhạc.

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
95 sau đó sử dụng để di chuyển thời gian trong bài kiểm tra đơn vị của bạn khi bạn kể một câu chuyện

Thêm nhập khẩu

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
96 ở đầu tệp

$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
3

Các getters được triển khai thông qua phương thức

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
97 của PHP. This enables you to access the value as if it was a property rather than a function call

$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
4

Các setters sau đây được triển khai thông qua phương thức

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
98 của PHP. Thật tốt khi lưu ý ở đây rằng không có setters nào, ngoại trừ rõ ràng là đặt múi giờ rõ ràng, sẽ thay đổi múi giờ của thể hiện. Cụ thể, việc đặt dấu thời gian sẽ không đặt múi giờ tương ứng thành UTC

$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
5

Nếu bạn đã quen thuộc với momentjs, bạn sẽ thấy các phương pháp tuần đều hoạt động giống nhau. Hầu hết chúng đều có biến thể iso{Method}. Các phương thức tuần tuân theo các quy tắc của ngôn ngữ hiện tại (ví dụ: với en_US, ngôn ngữ mặc định, ngày đầu tuần là Chủ nhật và tuần đầu tiên của năm là tuần chứa ngày 1 tháng 1). Các phương pháp của ISO tuân theo tiêu chuẩn ISO 8601, nghĩa là các tuần bắt đầu bằng Thứ Hai và tuần đầu tiên của năm là tuần chứa ngày 4 tháng Giêng

$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
6

Bạn có thể gọi bất kỳ đơn vị cơ sở nào dưới dạng setter hoặc một số setters được nhóm

$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
7

Bạn cũng có thể đặt ngày và giờ riêng biệt với các đối tượng DateTime/Carbon khác

$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
8

Hàm 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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
99 được triển khai. Điều này đã được thực hiện như một số hệ thống bên ngoài (ví dụ:. ) xác thực sự tồn tại của một thuộc tính trước khi sử dụng nó. Điều này được thực hiện bằng cách sử dụng phương pháp
$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.
00 hoặc
$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.
01. Bạn có thể đọc thêm về những điều này trên trang web PHP. , isset(), trống()

$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
9

Tất cả các phương thức

$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.
02 có sẵn đều dựa trên phương thức của lớp cơ sở DateTime. định dạng(). Bạn sẽ nhận thấy phương thức
$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.
03 được xác định cho phép in một thể hiện Carbon dưới dạng một chuỗi thời gian ngày tháng đẹp mắt khi được sử dụng trong ngữ cảnh chuỗi

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
0

Bạn cũng có thể đặt định dạng __toString() mặc định (mặc định là

$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.
04) được sử dụng khi xảy ra hiện tượng tung hứng kiểu

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
1

Là một phần của cài đặt

$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.
05 cũng có thể được sử dụng trong các nhà máy. Nó cũng có thể là một bao đóng, vì vậy bạn có thể chạy bất kỳ mã nào trên chuỗi truyền

Nếu bạn sử dụng Carbon 1 hoặc muốn áp dụng nó trên toàn cầu làm định dạng mặc định, bạn có thể sử dụng

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
2

Ghi chú. Để được hỗ trợ bản địa hóa, hãy xem phần

Sau đây là các hàm bao cho các định dạng phổ biến được cung cấp trong lớp DateTime

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
3_______1_______4

Bạn có thể sử dụng phương pháp

$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.
06 để chuyển đổi nhiều thứ thành một phiên bản
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
08 dựa trên một phiên bản nguồn nhất định được sử dụng làm tài liệu tham khảo khi cần. Nó trả về một thể hiện mới

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
5

So sánh đơn giản được cung cấp thông qua các chức năng sau. Hãy nhớ rằng việc so sánh được thực hiện theo múi giờ UTC nên mọi thứ không phải lúc nào cũng như vẻ ngoài của chúng

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
6

Các phương thức đó sử dụng phép so sánh tự nhiên được cung cấp bởi 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.
08, vì vậy tất cả chúng sẽ bỏ qua mili/micro giây trước PHP 7. 1, sau đó tính đến chúng bắt đầu bằng 7. 1

Để xác định xem phiên bản hiện tại có nằm giữa hai phiên bản khác hay không, bạn có thể sử dụng phương pháp được đặt tên phù hợp là ___90_______09 (hoặc bí danh ____

$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.
10). Tham số thứ ba cho biết liệu có nên thực hiện so sánh bằng. Giá trị mặc định là true xác định xem nó nằm giữa hay bằng các ranh giới

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
7

ồ. Bạn đã quên min() và max()? . Điều đó cũng được bao phủ bởi các phương pháp có tên thích hợp là

$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.
11 và
$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.
12 hoặc bí danh
$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.
13 và
$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.
14. Như thường lệ, tham số mặc định hiện tại nếu null được chỉ định

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
8

Để xử lý các trường hợp được sử dụng nhiều nhất, có một số chức năng trợ giúp đơn giản mà hy vọng là rõ ràng từ tên của chúng. Đối với các phương thức so sánh với

use Carbon\Carbon;
68 (ví dụ:. isToday()) theo một cách nào đó, thì
use Carbon\Carbon;
68 được tạo trong cùng múi giờ với ví dụ

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
9

DateTime mặc định cung cấp một số phương pháp khác nhau để dễ dàng cộng và trừ thời gian. Có

$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.
17,
$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.
18 và
$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.
19.
$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.
20 là phiên bản nâng cao của
$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.
17 có thể lấy chuỗi định dạng ngày/giờ kỳ diệu,
$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.
22, nó phân tích cú pháp và áp dụng sửa đổi trong khi
$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.
18 và
$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.
19 có thể lấy cùng loại chuỗi, khoảng thời gian (
$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.
25 hoặc
$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.
26) hoặc số lượng+đơn vị . Nhưng bạn vẫn có thể truy cập các phương thức gốc của lớp
$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.
27 bằng cách sử dụng
$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.
28 và
$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.
29

$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.";
}
0

Để giải trí, bạn cũng có thể chuyển các giá trị âm cho

$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.
30, trên thực tế, đó là cách thực hiện
$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.
31

P. S. Đừng lo lắng nếu bạn quên và sử dụng

$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.
32 hoặc
$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.
33, tôi hỗ trợ bạn;)

Theo mặc định, Carbon dựa trên hành vi DateTime của lớp cha bên dưới PHP. Kết quả là việc cộng hoặc trừ các tháng có thể bị tràn, ví dụ

$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.";
}
1

Kể từ Carbon 2, bạn có thể đặt hành vi tràn cục bộ cho từng phiên bản

$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.";
}
2

Điều này sẽ áp dụng cho các phương pháp

$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.
34,
$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.
35,
$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.
36,
$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.
37 và các phương pháp quý tương đương. Nhưng nó sẽ không áp dụng cho các đối tượng khoảng cách hoặc chuỗi như
$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.
38 hoặc
$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.
39

Trình trợ giúp tĩnh tồn tại nhưng không được dùng nữa. Nếu bạn chắc chắn cần áp dụng cài đặt chung hoặc hoạt động với phiên bản 1 của Carbon,

Bạn có thể ngăn tràn với

$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.
40

$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.";
}
3

Phương pháp

$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.
41 cho phép bạn biết nếu tràn hiện đang được kích hoạt

$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.";
}
4

Từ phiên bản 1. 23. 0, kiểm soát tràn cũng có sẵn trên các năm

$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.";
}
5

Bạn cũng có thể sử dụng

$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.
42,
$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.
43,
$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.
44 và
$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.
45 (hoặc các phương pháp số ít không có
$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.
46 thành "tháng") để cộng/phụ một cách rõ ràng các tháng có hoặc không có tràn bất kể chế độ hiện tại và tương tự cho bất kỳ đơn vị lớn hơn nào (quý, năm

$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.";
}
6

Điều tương tự có sẵn trong nhiều năm

Bạn cũng có thể kiểm soát tràn cho bất kỳ thiết bị nào khi làm việc với đầu vào không xác định

$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.";
}
7

Bất kỳ đơn vị có thể sửa đổi nào cũng có thể được chuyển làm đối số của các phương thức đó

$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.";
}
8

Khi

$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
08 kéo dài
$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.
27, nó kế thừa các phương thức của nó, chẳng hạn như
$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.
49 lấy đối tượng ngày thứ hai làm đối số và trả về một thể hiện ________90____25

Chúng tôi cũng cung cấp

$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.
51 hành động giống như
$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.
49 nhưng trả về một phiên bản
$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.
26. Kiểm tra để biết thêm thông tin. Carbon cũng thêm các phương pháp khác cho từng đơn vị, chẳng hạn như
$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.
54,
$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.
55, v.v. Tất cả các phương thức
$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.
51 và
$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.
57 và
$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.
58 đều có thể nhận 2 đối số tùy chọn. ngày để so sánh với (nếu thiếu, bây giờ được sử dụng thay thế) và một tùy chọn boolean tuyệt đối (
$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.
59 theo mặc định) làm cho phương thức trả về một giá trị tuyệt đối bất kể ngày nào lớn hơn ngày kia. Nếu được đặt thành
$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.
60, nó sẽ trả về giá trị âm khi phiên bản mà phương thức được gọi lớn hơn ngày được so sánh (đối số đầu tiên hoặc bây giờ). Lưu ý rằng nguyên mẫu
$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.
49 khác. đối số đầu tiên của nó (ngày) là bắt buộc và đối số thứ hai của nó (tùy chọn tuyệt đối) mặc định là
$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.
60

Các hàm này luôn trả về tổng chênh lệch được biểu thị trong thời gian đã chỉ định được yêu cầu. Điều này khác với hàm

$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.
49 của lớp cơ sở trong đó khoảng thời gian 122 giây sẽ được trả về là 2 phút 2 giây thông qua một thể hiện
$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.
25. Hàm
$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.
65 sẽ trả về 2 trong khi
$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.
66 sẽ trả về 122. Tất cả các giá trị được cắt bớt và không được làm tròn. Mỗi chức năng bên dưới có một tham số đầu tiên mặc định là phiên bản Carbon để so sánh hoặc null nếu bạn muốn sử dụng
use Carbon\Carbon;
68. Tham số thứ 2 lại là tùy chọn và cho biết bạn muốn giá trị trả về là giá trị tuyệt đối hay giá trị tương đối có thể có dấu
$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.
68 (âm) nếu ngày được chuyển vào nhỏ hơn phiên bản hiện tại. Điều này sẽ mặc định là true, trả về giá trị tuyệt đối

$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.";
}
9

Các phương pháp này có kết quả cắt ngắn. Điều đó có nghĩa là

$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.
69 trả về 1 cho bất kỳ sự khác biệt nào giữa 1 được bao gồm và 2 bị loại trừ. Nhưng các phương pháp tương tự có sẵn cho kết quả float

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
0

⚠️ Lưu ý quan trọng về thời gian tiết kiệm ánh sáng ban ngày (DST), theo mặc định PHP DateTime không tính đến DST, điều đó có nghĩa là ví dụ một ngày chỉ có 23 giờ như ngày 30 tháng 3 năm 2014 ở London sẽ được tính là 24 giờ

⚠️ Hãy cẩn thận với

$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.
70, điều này có thể mang lại cho bạn kết quả thấp hơn (_______90_______71) trong khoảng thời gian có nhiều ngày hơn (_______90_______72) do số ngày trong tháng có thể thay đổi (đặc biệt là tháng 2). Theo mặc định, chúng tôi dựa vào kết quả của DateTime. diff nhạy cảm với tràn. Xem vấn đề #2264 để biết các phép tính thay thế

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
1

Carbon cũng tuân theo hành vi này để thêm/phụ/diff giây/phút/giờ. Nhưng chúng tôi cung cấp các phương pháp để hoạt động với giờ thực bằng cách sử dụng dấu thời gian

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
2

Theo cách tương tự, bạn có thể sử dụng

$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.
73 và
$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.
74 trên bất kỳ đơn vị nào

Ngoài ra còn có các chức năng lọc đặc biệt

$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.
75,
$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.
76 và
$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.
77, để giúp bạn lọc sự khác biệt theo ngày, giờ hoặc khoảng thời gian tùy chỉnh. Ví dụ để đếm ngày cuối tuần giữa hai trường hợp

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
3

Tất cả phương thức diffIn*Filtered lấy 1 bộ lọc có thể gọi được làm tham số bắt buộc và đối tượng ngày làm tham số thứ hai tùy chọn, nếu thiếu, bây giờ được sử dụng. Bạn cũng có thể chuyển true làm tham số thứ ba để nhận các giá trị tuyệt đối

Để xử lý nâng cao các ngày trong tuần/cuối tuần, hãy sử dụng các công cụ sau

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
4

Con người đọc

$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.
78 dễ dàng hơn so với 30 ngày trước. Đây là một chức năng phổ biến được thấy trong hầu hết các thư viện ngày, vì vậy tôi nghĩ rằng tôi cũng sẽ thêm nó vào đây. Đối số duy nhất cho hàm là đối tượng Carbon khác để chống lại, và tất nhiên, nó mặc định là
use Carbon\Carbon;
68 nếu không được chỉ định

Phương thức này sẽ thêm một cụm từ sau giá trị chênh lệch so với phiên bản và phiên bản được truyền trong phiên bản. Có 4 khả năng

  • Khi so sánh một giá trị trong quá khứ với giá trị mặc định hiện tại
  • Khi so sánh một giá trị trong tương lai với giá trị mặc định hiện tại
    • 1 giờ kể từ bây giờ
    • 5 tháng kể từ bây giờ
  • Khi so sánh một giá trị trong quá khứ với một giá trị khác
    • 1 giờ trước
    • 5 tháng trước
  • Khi so sánh một giá trị trong tương lai với một giá trị khác
    • 1 giờ sau
    • 5 tháng sau

Bạn cũng có thể chuyển

$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.
80 làm tham số thứ 2 để xóa các công cụ sửa đổi trước đây, kể từ bây giờ, v.v.
$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.
81,
$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.
82 để nhận các công cụ sửa đổi trước hoặc sau đó,
$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.
83 để nhận các công cụ sửa đổi trước hoặc sau hoặc
$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.
84 (chế độ mặc định) để nhận các công cụ sửa đổi trước/từ bây giờ nếu đối số 2 giây là null hoặc trước/sau nếu không

Bạn có thể chuyển

$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.
59 làm tham số thứ 3 để sử dụng cú pháp ngắn nếu có ở ngôn ngữ được sử dụng.
$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.
86

Bạn có thể chuyển một số từ 1 đến 6 làm tham số thứ 4 để nhận được sự khác biệt ở nhiều phần (khác biệt chính xác hơn).

$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.
87

Phiên bản

$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.
88 có thể là một DateTime, một phiên bản Carbon hoặc bất kỳ đối tượng nào triển khai DateTimeInterface, nếu một chuỗi được truyền, nó sẽ được phân tích cú pháp để lấy một phiên bản Carbon và nếu
use Carbon\Carbon;
86 được truyền, thay vào đó,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
88 sẽ được sử dụng

Để tránh có quá nhiều đối số và trộn lẫn thứ tự, bạn có thể sử dụng các phương thức dài dòng

  • $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.
    
    91
  • $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.
    
    92
  • $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.
    
    93
  • $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.
    
    94
  • $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.
    
    95
  • $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.
    
    96
  • $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.
    
    97
  • $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.
    
    98

Tái bút. Các đối số

$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.
88 và
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
00 có thể được hoán đổi khi cần

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
5

Bạn cũng có thể thay đổi ngôn ngữ của chuỗi bằng cách sử dụng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
01 trước lệnh gọi diffForHumans(). Xem phần để biết thêm chi tiết

kể từ 2. 9. 0 tham số diffForHumans() có thể được truyền dưới dạng một mảng

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
6

Nếu đối số bị bỏ qua hoặc được đặt thành

use Carbon\Carbon;
86, thì chỉ có
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
03 được bật. tùy chọn có sẵn là

  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    04 /
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    05 /
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    06 (không có gì theo mặc định). mỗi lần chỉ có thể sử dụng một trong 3 cái (các cái khác bị bỏ qua) và nó yêu cầu phải đặt ____141_______07. Theo mặc định, một khi khác biệt có các phần như cài đặt
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    07 được yêu cầu và bỏ qua tất cả các đơn vị còn lại
    • Nếu
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      04 được bật, các đơn vị còn lại được tính tổng và nếu chúng >= 0. 5 của đơn vị cuối cùng của khác biệt, đơn vị này sẽ được làm tròn thành giá trị trên
    • Nếu
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      05 được bật, các đơn vị còn lại được tính tổng và nếu chúng > 0 của đơn vị cuối cùng của khác biệt, đơn vị này sẽ được làm tròn thành giá trị trên
    • Nếu
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      06 được bật, đơn vị khác biệt cuối cùng được làm tròn xuống. Nó không tạo ra sự khác biệt so với hành vi mặc định cho
      $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(8) "Sunday 1"
      var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
      // 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(11) "Saturday 31"
      var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
      // 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)
      
      16 vì khoảng thời gian không thể bị tràn, nhưng tùy chọn này có thể cần thiết khi được sử dụng với
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      13 (và các khoảng thời gian không được kiểm tra có thể có 60 phút trở lên, 24 giờ trở lên, v.v. ). Ví dụ.
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      14 trả về
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      15 trong khi
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      16 trả về
      $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
      // You can create Carbon or CarbonImmutable instance from:
      //   - string representation
      //   - integer timestamp
      //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
      // All those are available right in the constructor, other creator methods can be found
      // in the "Reference" panel searching for "create".
      
      17
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    03 (được bật theo mặc định). biến khác biệt trống thành 1 giây
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    19 bị tắt theo mặc định). biến diff từ bây giờ đến bây giờ thành "ngay bây giờ"
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    20 (disabled by default). turns "1 day from now/ago" to "yesterday/tomorrow"
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    21 (disabled by default). turns "2 days from now/ago" to "before yesterday/after
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    22 (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 = 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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
23

You also can use

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
24,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
25,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
26 to change the default options and
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
27 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

  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    28 (alias of diffForHumans)
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    29 (alias of diffForHumans)
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    30 (inverse result, swap before and future diff)
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    31 (alias of to)
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    32 (alias of from with first argument omitted unless the first argument is a
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    33, now used instead), for semantic usage. produce an "3 hours from now"-like string with dates in the future
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    34 (alias of fromNow), for semantic usage. produce an "3 hours ago"-like string with dates in the past
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    35 (alias of to with first argument omitted, now used instead)
  • $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    36 gọi diffForHumans với các tùy chọn
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    37 (được phân tách bằng hôn mê),
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    38 (không có từ "trước"/"từ bây giờ"/"trước"/"sau"),
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    39 (không có từ "ngay bây giờ"/"hôm qua"/"ngày mai"

Nhóm các phương thức này thực hiện các sửa đổi hữu ích đối với phiên bản hiện tại. Hầu hết trong số họ là tự giải thích từ tên của họ. hoặc ít nhất là nên. Bạn cũng sẽ nhận thấy rằng các phương thức startOfXXX(), next() và previous() đặt thời gian là 00. 00. 00 và các phương thức endOfXXX() đặt thời gian thành 23. 59. 59 cho đơn vị lớn hơn ngày

Điều duy nhất hơi khác một chút là chức năng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
41. Nó di chuyển phiên bản của bạn đến ngày ở giữa giữa chính nó và đối số Carbon được cung cấp

Phương pháp

$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.
17 bản địa mạnh mẽ của
$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.
27 có sẵn nguyên vẹn. Nhưng chúng tôi cũng cung cấp một phiên bản nâng cao của nó.
$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.
20 cho phép một số cú pháp bổ sung như
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
45 và được gọi nội bộ bởi
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
46 và
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
47

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
7

Làm tròn cũng có sẵn cho bất kỳ đơn vị

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
8

Các hằng số sau được định nghĩa trong lớp Carbon

$now = Carbon::now();
echo $now;                               // 2022-12-31 15:56:18
echo "\n";
$today = Carbon::today();
echo $today;                             // 2022-12-31 00:00:00
echo "\n";
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow;                          // 2023-01-01 00:00:00
echo "\n";
$yesterday = Carbon::yesterday();
echo $yesterday;                         // 2022-12-30 00:00:00
9_______15_______0

Các phiên bản Carbon có thể được đánh số thứ tự (bao gồm CarbonImmutable, CarbonInterval và CarbonPeriod)

$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";
1

Các phiên bản Carbon có thể được mã hóa và giải mã từ JSON. Since the version 2. 4, chúng tôi yêu cầu rõ ràng phần mở rộng PHP JSON. Nó sẽ không ảnh hưởng gì vì tiện ích mở rộng này được đóng gói theo mặc định với PHP. Nếu tiện ích mở rộng bị tắt, hãy lưu ý rằng bạn sẽ bị khóa trên 2. 3. Nhưng bạn vẫn có thể sử dụng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
48 trên bản cập nhật/cài đặt của nhà soạn nhạc để nâng cấp, sau đó điền vào giao diện
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
49 bị thiếu bằng cách bao gồm JsonSerializable. 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";
2

Bạn có thể sử dụng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
50 để tùy chỉnh số thứ tự

$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";
3

Nếu bạn muốn áp dụng điều này trên toàn cầu, trước tiên hãy cân nhắc sử dụng nhà máy, hoặc nếu bạn sử dụng Carbon 1, bạn có thể sử dụng

$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";
4

Phương thức

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
51 cho phép bạn gọi hàm được cung cấp cho
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
52 hoặc kết quả của
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
53 nếu không chỉ định tuần tự hóa tùy chỉnh

Bạn có thể quen thuộc với khái niệm macro nếu bạn đã quen làm việc với Laravel và các đối tượng như hoặc. Các macro carbon hoạt động giống như Laravel

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
54 Trait

Gọi phương thức

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
55 với tên macro của bạn làm đối số thứ nhất và đóng làm đối số thứ hai. Điều này sẽ làm cho hành động đóng có sẵn trên tất cả các phiên bản Carbon

$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";
5

Lưu ý rằng phần đóng đứng trước

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
56 và sử dụng
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
57 (có sẵn từ phiên bản 2. 25. 0) thay vì
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
58. Đây là cách tiêu chuẩn để tạo các macro Carbon và cách này cũng áp dụng cho các macro trên các lớp khác (
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
59,
$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.
26 và
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
61)

Bằng cách làm theo mẫu này, bạn đảm bảo rằng các nhà phát triển khác trong nhóm của bạn (và bạn trong tương lai) có thể tin cậy một cách an toàn vào xác nhận.

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
62 tương đương với
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
63. Điều này làm cho việc sử dụng macro trở nên nhất quán và có thể dự đoán được, đồng thời đảm bảo cho nhà phát triển rằng bất kỳ macro nào cũng có thể được gọi một cách an toàn ở dạng tĩnh hoặc động

Điều đáng buồn là IDE sẽ không phải là phương thức macro của bạn (không có tính năng tự động hoàn thành cho phương thức

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
64 trong ví dụ trên). Nhưng đó không còn là vấn đề nhờ công cụ CLI của chúng tôi. carbon-cli cho phép bạn tạo các tệp trợ giúp IDE cho mixin và macro của bạn

Macro là công cụ hoàn hảo để xuất ngày với một số cài đặt hoặc tùy chọn người dùng

$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";
6

Macro cũng có thể được nhóm trong các lớp và được áp dụng với

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
65

$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";
7

Kể từ Carbon 2. 23. 0, cũng có thể rút ngắn cú pháp mixin bằng cách sử dụng các đặc điểm

$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";
8

Bạn có thể kiểm tra xem macro (bao gồm cả mixin) có khả dụng với

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
66 hay không và truy xuất lệnh đóng macro với
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
67

$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";
9

Một macro bắt đầu bằng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
68 theo sau là một chữ cái viết hoa sẽ tự động cung cấp một bộ thu động trong khi một macro bắt đầu bằng
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
69 và theo sau là một chữ cái viết hoa sẽ cung cấp một bộ thiết lập động

use Carbon\Carbon;
00

Bạn cũng có thể chặn bất kỳ cuộc gọi nào khác bằng macro chung

use Carbon\Carbon;
01

Và đoán xem?

use Carbon\Carbon;
02____0_______03

Chúng tôi cung cấp tiện ích mở rộng PHPStan ngay lập tức mà bạn có thể đưa vào phpstan của mình. đèn neon

use Carbon\Carbon;
04

Nếu bạn đang sử dụng Laravel, bạn có thể cân nhắc sử dụng larastan, hỗ trợ đầy đủ các tính năng của Laravel trong PHPStan (bao gồm cả Carbon macro). Ngoài ra, để bao gồm tệp neon, bạn có thể cài đặt phpstan/extension-installer

use Carbon\Carbon;
05

Sau đó, thêm tệp nơi macro Carbon và mixin của bạn được xác định trong mục bootstrapFiles

use Carbon\Carbon;
06

Kiểm tra cmixin/thời gian làm việc (bao gồm cả cmixin/ngày làm việc) để xử lý cả ngày lễ và giờ làm việc với nhiều tính năng nâng cao

use Carbon\Carbon;
07

Tín dụng. meteguerlek (#1191)

use Carbon\Carbon;
08

Tín dụng. afrojuju1 (#1063)

use Carbon\Carbon;
09

Tín dụng. andreisena, 36864 (#1052)

Kiểm tra cmixin/ngày làm việc để có trình xử lý ngày lễ hoàn chỉnh hơn

use Carbon\Carbon;
10

Tín dụng. vẽ lại (#132)

use Carbon\Carbon;
11

Tín dụng. thiagocordeiro (#927)

Trong khi sử dụng macro là cách được đề xuất để thêm các phương thức hoặc hành vi mới vào Carbon, bạn có thể tiến xa hơn và mở rộng chính lớp đó, điều này cho phép một số cách thay thế để ghi đè lên các phương thức chính;

use Carbon\Carbon;
12

Macro sau đây cho phép bạn chọn múi giờ chỉ bằng tên thành phố (bỏ qua lục địa). Hoàn hảo để làm cho bài kiểm tra đơn vị của bạn trôi chảy hơn

use Carbon\Carbon;
13

Lớp CarbonInterval được kế thừa từ lớp DateInterval của PHP

Phương pháp
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
72 và
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
73 mong đợi các số cho mỗi đơn vị theo cùng một thứ tự hơn
use Carbon\Carbon;
83 và có thể được sử dụng một cách thuận tiện với PHP 8

use Carbon\Carbon;
17

Nếu bạn thấy mình kế thừa một phiên bản

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
75 từ một thư viện khác, đừng lo. Bạn có thể tạo một phiên bản
$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.
26 thông qua hàm
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
09 thân thiện

use Carbon\Carbon;
18

Và ngược lại, bạn có thể trích xuất một

$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.
25 thô từ
$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.
26 và thậm chí bỏ nó vào bất kỳ lớp nào mở rộng
$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.
25

use Carbon\Carbon;
19

Bạn có thể so sánh các khoảng thời gian theo cách giống như các đối tượng Carbon, sử dụng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
81,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
82
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
83,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
84,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
85,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
86,
$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.
09,
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
88, v.v.

Những người trợ giúp khác, nhưng hãy lưu ý rằng việc triển khai cung cấp cho những người trợ giúp để xử lý hàng tuần nhưng chỉ tiết kiệm được vài ngày. Số tuần được tính dựa trên tổng số ngày của phiên bản hiện tại

use Carbon\Carbon;
20

CarbonInterval mở rộng DateInterval và bạn có thể tạo cả hai bằng cách sử dụng

use Carbon\Carbon;
21

Khoảng carbon có thể được tạo ra từ các chuỗi thân thiện với con người nhờ phương pháp

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
89

use Carbon\Carbon;
22

Hoặc tạo nó từ một đối tượng

$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.
25 /
$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.
26 khác

use Carbon\Carbon;
23

Lưu ý tháng viết tắt "mo" để phân biệt với phút và toàn bộ cú pháp không phân biệt chữ hoa chữ thường

Nó cũng có một

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
92 tiện dụng, được ánh xạ dưới dạng triển khai
$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.
03, in khoảng thời gian cho con người

use Carbon\Carbon;
24

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
94 cho phép các đối số tùy chọn giống như
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
95 ngoại trừ
$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
00 được đặt thành -1 (không giới hạn) theo mặc định.

Như bạn có thể thấy, bạn có thể thay đổi ngôn ngữ của chuỗi bằng cách sử dụng

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
98

Đối với Carbon, bạn có thể sử dụng phương thức tạo để trả về một phiên bản mới của CarbonInterval từ các khoảng hoặc chuỗi khác

use Carbon\Carbon;
25

Các phương pháp cộng, phụ (hoặc trừ), lần, chia sẻ, nhân và chia cho phép thực hiện các phép tính khoảng thời gian tiếp theo

use Carbon\Carbon;
26

Bạn nhận được phép tính thuần túy từ đơn vị đầu vào của mình theo đơn vị. Để xếp từng phút thành giờ, giờ thành ngày, v.v. Sử dụng phương pháp xếp tầng

use Carbon\Carbon;
27

Các yếu tố mặc định là

  • 1 phút = 60 giây
  • 1 giờ = 60 phút
  • 1 ngày = 24 giờ
  • 1 tuần = 7 ngày
  • 1 tháng = 4 tuần
  • 1 năm = 12 tháng

CarbonIntervals không mang ngữ cảnh nên chúng không thể chính xác hơn (không có DST, không có năm nhuận, không xem xét độ dài tháng hoặc độ dài năm thực). Nhưng bạn hoàn toàn có thể tùy chỉnh các yếu tố đó. Ví dụ để xử lý nhật ký thời gian làm việc

use Carbon\Carbon;
28

Có thể chuyển đổi một khoảng thời gian thành một đơn vị nhất định (sử dụng các yếu tố xếp tầng được cung cấp)

use Carbon\Carbon;
29

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
99 phương thức và thuộc tính cần các khoảng thời gian xếp tầng, nếu khoảng thời gian của bạn có thể bị tràn, hãy xếp tầng chúng trước khi gọi các tính năng này

use Carbon\Carbon;
30

Bạn cũng có thể lấy thông số ISO 8601 của khoảng thời gian với ________ 162 ______ 00

use Carbon\Carbon;
31

Cũng có thể lấy nó từ một đối tượng DateInterval kể từ trình trợ giúp tĩnh

use Carbon\Carbon;
32

Danh sách khoảng thời gian ngày có thể được sắp xếp nhờ các phương pháp

$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
01 và
$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
02

use Carbon\Carbon;
33

Ngoài các khoảng thời gian cố định, Khoảng thời gian động có thể được mô tả bằng chức năng bước từ ngày này sang ngày khác

use Carbon\Carbon;
34

Bạn có thể truy cập và sửa đổi định nghĩa bước đóng bằng cách sử dụng

$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
03 và
$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
04 (trình thiết lập có thể lấy
use Carbon\Carbon;
86 để xóa nó để nó trở thành một khoảng thời gian cố định đơn giản. Và miễn là khoảng thời gian có một bước, nó sẽ được ưu tiên hơn tất cả các đơn vị cố định mà nó chứa

Bạn có thể gọi

$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
06 để áp dụng khoảng thời gian động hoặc tĩnh hiện tại cho một ngày (
$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.
27,
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
08 hoặc những ngày không thay đổi) một cách tích cực (hoặc chuyển một cách tiêu cực
$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.
59 làm đối số thứ hai)

use Carbon\Carbon;
35

Kết xuất các giá trị khoảng dưới dạng một mảng với

use Carbon\Carbon;
36

Cuối cùng, một phiên bản CarbonInterval có thể được chuyển đổi thành một phiên bản CarbonPeriod bằng cách gọi

$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
10 với các đối số bổ sung

Tôi nghe bạn hỏi phiên bản CarbonPeriod là gì. Ồ. Chuyển đổi hoàn hảo sang chương tiếp theo của chúng tôi

CarbonPeriod là phiên bản DatePeriod thân thiện với con người với nhiều phím tắt

use Carbon\Carbon;
37

Một CarbonPeriod có thể được xây dựng theo một số cách

  • ngày bắt đầu, ngày kết thúc và khoảng thời gian tùy chọn (theo mặc định là 1 ngày),
  • ngày bắt đầu, số lần lặp lại và khoảng thời gian tùy chọn,
  • thông số kỹ thuật khoảng ISO 8601,
  • từ một
    $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
    
    11 hoặc
    $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
    // You can create Carbon or CarbonImmutable instance from:
    //   - string representation
    //   - integer timestamp
    //   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
    // All those are available right in the constructor, other creator methods can be found
    // in the "Reference" panel searching for "create".
    
    61 khác bằng cách sử dụng
    $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
    
    13 hoặc đơn giản là sử dụng
    $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
    
    14

Ngày có thể được cung cấp dưới dạng phiên bản DateTime/Carbon, các chuỗi tuyệt đối như "2007-10-15 15. 00" hoặc chuỗi tương đối, ví dụ "thứ hai tới". Khoảng thời gian có thể được cung cấp dưới dạng ví dụ DateInterval/CarbonInterval, thông số kỹ thuật khoảng thời gian ISO 8601 như "P4D" hoặc chuỗi có thể đọc được của con người, ví dụ: "4 ngày"

Hàm tạo mặc định và các phương thức

use Carbon\Carbon;
83 rất dễ hiểu về loại đối số và thứ tự, vì vậy nếu bạn muốn chính xác hơn thì nên sử dụng cú pháp thông thạo. Mặt khác, bạn có thể chuyển mảng giá trị động cho
$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
16, nó sẽ thực hiện công việc xây dựng một thể hiện mới với mảng đã cho dưới dạng danh sách các đối số

CarbonPeriod triển khai giao diện Iterator. Điều đó có nghĩa là nó có thể được chuyển trực tiếp đến vòng lặp

$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
17

use Carbon\Carbon;
38

Các tham số có thể được sửa đổi trong quá trình lặp lại

use Carbon\Carbon;
39

Giống như DatePeriod, CarbonPeriod hỗ trợ

Lưu ý rằng DatePeriod gốc coi số lần lặp lại là số lần lặp lại khoảng thời gian. Do đó, nó sẽ cho một kết quả ít hơn khi loại trừ ngày bắt đầu. Việc giới thiệu các bộ lọc tùy chỉnh trong CarbonPeriod khiến việc biết số lượng kết quả trở nên khó khăn hơn. Vì lý do đó, chúng tôi đã thay đổi cách triển khai một chút và các lần lặp lại được coi là giới hạn chung cho số ngày được trả về

use Carbon\Carbon;
40

Bạn có thể truy xuất dữ liệu từ khoảng thời gian với nhiều getters

use Carbon\Carbon;
41

Các getters bổ sung cho phép bạn truy cập các kết quả dưới dạng một mảng

use Carbon\Carbon;
42

Lưu ý rằng nếu bạn có ý định làm việc bằng cách sử dụng các hàm trên, bạn nên lưu trữ kết quả của lệnh gọi

$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
18 vào một biến và sử dụng nó thay vào đó, bởi vì mỗi lệnh gọi thực hiện một lần lặp đầy đủ trong nội bộ

Để thay đổi các tham số, bạn có thể sử dụng các phương thức setter

use Carbon\Carbon;
43

Bạn có thể thay đổi các tùy chọn bằng cách sử dụng

$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
19 để thay thế tất cả các tùy chọn nhưng bạn cũng có thể thay đổi chúng một cách riêng biệt

use Carbon\Carbon;
44

Bạn có thể kiểm tra xem 2 kỳ có trùng nhau hay không

use Carbon\Carbon;
45

Như đã đề cập trước đó, theo tiêu chuẩn ISO 8601, số lần lặp lại là số lần khoảng thời gian phải được lặp lại. Do đó, DatePeriod gốc sẽ thay đổi số lượng ngày được trả về tùy thuộc vào việc loại trừ ngày bắt đầu. Trong khi đó, CarbonPeriod dễ tha thứ hơn về mặt đầu vào và cho phép các bộ lọc tùy chỉnh, coi các lần lặp lại là giới hạn tổng thể cho số ngày được trả lại

use Carbon\Carbon;
46

Có thể dễ dàng lọc các ngày được trả về bởi DatePeriod. Chẳng hạn, có thể sử dụng các bộ lọc để bỏ qua một số ngày nhất định hoặc chỉ lặp lại trong các ngày làm việc hoặc cuối tuần. Hàm lọc phải trả về

$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.
59 để chấp nhận một ngày,
$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.
60 để bỏ qua nó nhưng tiếp tục tìm kiếm hoặc
$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
22 để kết thúc quá trình lặp lại

use Carbon\Carbon;
47

Bạn cũng có thể bỏ qua một hoặc nhiều giá trị bên trong vòng lặp

use Carbon\Carbon;
48

$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
23 cho phép bạn truy xuất tất cả các bộ lọc được lưu trữ trong một khoảng thời gian. Nhưng hãy lưu ý giới hạn số lần lặp lại và ngày kết thúc sẽ xuất hiện trong mảng được trả về khi chúng được lưu trữ nội bộ dưới dạng bộ lọc

use Carbon\Carbon;
49

Các bộ lọc được lưu trữ trong ngăn xếp và có thể được quản lý bằng một bộ phương thức đặc biệt

use Carbon\Carbon;
50

Thứ tự thêm bộ lọc có thể ảnh hưởng đến hiệu suất và kết quả, vì vậy bạn có thể sử dụng

$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
24 để thêm bộ lọc vào cuối ngăn xếp; . Bạn thậm chí có thể sử dụng
$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
26 để thay thế tất cả các bộ lọc. Lưu ý rằng bạn sẽ phải giữ đúng định dạng của ngăn xếp và nhớ về các bộ lọc nội bộ cho giới hạn lặp lại và ngày kết thúc. Ngoài ra, bạn có thể sử dụng phương pháp
$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
27 rồi thêm từng bộ lọc mới

Ví dụ: khi bạn thêm bộ lọc tùy chỉnh giới hạn số ngày thử, kết quả sẽ khác nếu bạn thêm bộ lọc đó trước hoặc sau bộ lọc ngày trong tuần

use Carbon\Carbon;
51

Lưu ý rằng bộ lọc lặp lại tích hợp không hoạt động theo cách này. Thay vào đó, nó dựa trên khóa hiện tại chỉ được tăng một lần cho mỗi mục, bất kể phải kiểm tra bao nhiêu ngày trước khi tìm thấy ngày hợp lệ. Thủ thuật này làm cho nó hoạt động như nhau nếu bạn đặt nó ở đầu hoặc ở cuối ngăn xếp

Một số bí danh đã được thêm vào để đơn giản hóa việc xây dựng CarbonPeriod

use Carbon\Carbon;
52

CarbonPeriod có thể dễ dàng chuyển đổi thành chuỗi có thể đọc được của con người và thông số kỹ thuật ISO 8601

use Carbon\Carbon;
53

Theo mặc định, sử dụng và trả về phiên bản Carbon, nhưng bạn có thể dễ dàng đặt/lấy lớp ngày để sử dụng để lấy ngày không thay đổi chẳng hạn hoặc bất kỳ lớp nào triển khai CarbonInterface

use Carbon\Carbon;
54

CarbonPeriod có các phương thức trợ giúp

$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
28 và
$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
29

use Carbon\Carbon;
55

Như tất cả các lớp Carbon khác,

$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
// You can create Carbon or CarbonImmutable instance from:
//   - string representation
//   - integer timestamp
//   - DateTimeInterface instance (that includes DateTime, DateTimeImmutable or an other Carbon instance)
// All those are available right in the constructor, other creator methods can be found
// in the "Reference" panel searching for "create".
61 có một phương thức
$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
31 để chuyển đổi nó

use Carbon\Carbon;
56

Bạn có thể kiểm tra xem thời gian có tự theo sau không. Period A follows period B if the first iteration date of B equals to the last iteration date of A + the interval of A. Ví dụ:

$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
32 theo sau
$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
33 (giả sử cả đầu và cuối đều không bị loại trừ thông qua tùy chọn cho các khoảng thời gian đó và giả sử các khoảng thời gian đó là khoảng thời gian (1 ngày)

use Carbon\Carbon;
57

Phương thức

$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
34 cho phép bạn kiểm tra xem một ngày có nằm trong khoảng thời gian không

use Carbon\Carbon;
58

So sánh bao gồm bắt đầu và kết thúc trừ khi bạn loại trừ chúng trong tùy chọn và đối với nó liên quan đến

$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
34, việc loại trừ chỉ loại trừ ngày chính xác, vì vậy

use Carbon\Carbon;
59

You can use start/end comparisons methods (that ignore exclusions) for more precise comparisons

  • $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
    
    36 start == date
  • $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
    
    37 start < date
  • $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
    
    38 bắt đầu <= ngày
  • $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
    
    39 bắt đầu > ngày
  • $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
    
    40 bắt đầu >= ngày
  • $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
    
    41 end == date
  • $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
    
    42 end < date
  • $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
    
    43 end <= date
  • $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
    
    44 end > date
  • $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
    
    45 end >= date
  • $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
    
    46 start <= now
  • $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
    
    47 end <= now
  • $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
    
    48 started but not ended

Starting with Carbon 2, timezones are now handled with a dedicated class

$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
49 extending DateTimeZone

use Carbon\Carbon;
60

The default timezone is given by date_default_timezone_get so it will be driven by the INI settings but you really should override it at application level using date_default_timezone_set and you should set it to

$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
50, if you're temped to or already use an other timezone as default, please read the following article. Always Use UTC Dates And Times

It explains why UTC is a reliable standard. And this best-practice is even more important in PHP because the PHP DateTime API has many bugs with offsets changes and DST timezones. Some of them appeared on minor versions and even on patch versions (so you can get different results running the same code on PHP 7. 1. 7 and 7. 1. 8 chẳng hạn) và một số lỗi thậm chí chưa được sửa. So we highly recommend to use UTC everywhere and only change the timezone when you want to display a date. See our

While, region timezone ("Continent/City") can have DST and so have variable offset during the year, offset timezone have constant fixed offset

use Carbon\Carbon;
61

You also can convert region timezones to offset timezones and reciprocally

use Carbon\Carbon;
62

Bạn có thể tạo một

$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
49 từ các giá trị hỗn hợp bằng cách sử dụng phương pháp
$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(8) "Sunday 1"
var_dump($modifiedMutable->isoFormat('dddd D'));     // string(8) "Sunday 1"
// 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(11) "Saturday 31"
var_dump($modifiedImmutable->isoFormat('dddd D'));   // string(8) "Sunday 1"
// 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)
09

use Carbon\Carbon;
63

The same way,

$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
53 return
$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.
60 if you pass an incorrect value (such as a negative month) but it throws an exception in strict mode.
$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
55 is like
use Carbon\Carbon;
83 but throws an exception even if not in strict mode

Nếu bạn định chuyển từ Carbon 1 sang Carbon 2, vui lòng lưu ý những thay đổi vi phạm sau đây mà bạn nên quan tâm