1
|
# Carbon
|
2
|
|
3
|
[](https://packagist.org/packages/nesbot/carbon)
|
4
|
[](https://packagist.org/packages/nesbot/carbon)
|
5
|
[](https://travis-ci.org/briannesbitt/Carbon)
|
6
|
[](https://styleci.io/repos/5724990)
|
7
|
[](https://codecov.io/github/briannesbitt/Carbon?branch=master)
|
8
|
[](https://php-eye.com/package/nesbot/carbon)
|
9
|
[](https://github.com/phpstan/phpstan)
|
10
|
|
11
|
A simple PHP API extension for DateTime. [http://carbon.nesbot.com](http://carbon.nesbot.com)
|
12
|
|
13
|
```php
|
14
|
use Carbon\Carbon;
|
15
|
|
16
|
printf("Right now is %s", Carbon::now()->toDateTimeString());
|
17
|
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver')); //implicit __toString()
|
18
|
$tomorrow = Carbon::now()->addDay();
|
19
|
$lastWeek = Carbon::now()->subWeek();
|
20
|
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);
|
21
|
|
22
|
$officialDate = Carbon::now()->toRfc2822String();
|
23
|
|
24
|
$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;
|
25
|
|
26
|
$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');
|
27
|
|
28
|
$worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT');
|
29
|
|
30
|
// Don't really want to die so mock now
|
31
|
Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1));
|
32
|
|
33
|
// comparisons are always done in UTC
|
34
|
if (Carbon::now()->gte($worldWillEnd)) {
|
35
|
die();
|
36
|
}
|
37
|
|
38
|
// Phew! Return to normal behaviour
|
39
|
Carbon::setTestNow();
|
40
|
|
41
|
if (Carbon::now()->isWeekend()) {
|
42
|
echo 'Party!';
|
43
|
}
|
44
|
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'
|
45
|
|
46
|
// ... but also does 'from now', 'after' and 'before'
|
47
|
// rolling up to seconds, minutes, hours, days, months, years
|
48
|
|
49
|
$daysSinceEpoch = Carbon::createFromTimestamp(0)->diffInDays();
|
50
|
```
|
51
|
|
52
|
## Installation
|
53
|
|
54
|
### With Composer
|
55
|
|
56
|
```
|
57
|
$ composer require nesbot/carbon
|
58
|
```
|
59
|
|
60
|
```json
|
61
|
{
|
62
|
"require": {
|
63
|
"nesbot/carbon": "~1.21"
|
64
|
}
|
65
|
}
|
66
|
```
|
67
|
|
68
|
```php
|
69
|
<?php
|
70
|
require 'vendor/autoload.php';
|
71
|
|
72
|
use Carbon\Carbon;
|
73
|
|
74
|
printf("Now: %s", Carbon::now());
|
75
|
```
|
76
|
|
77
|
<a name="install-nocomposer"/>
|
78
|
|
79
|
### Without Composer
|
80
|
|
81
|
Why are you not using [composer](http://getcomposer.org/)? Download [Carbon.php](https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Carbon.php) from the repo and save the file into your project path somewhere.
|
82
|
|
83
|
```php
|
84
|
<?php
|
85
|
require 'path/to/Carbon.php';
|
86
|
|
87
|
use Carbon\Carbon;
|
88
|
|
89
|
printf("Now: %s", Carbon::now());
|
90
|
```
|
91
|
|
92
|
## Docs
|
93
|
|
94
|
[http://carbon.nesbot.com/docs](http://carbon.nesbot.com/docs)
|