This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.
This package is abandoned and no longer maintained. The author suggests using the jenssegers/date package instead.

A simple API extension for DateTime. This is a modernized nesbot/carbon package.

1.22.1 2016-07-13 02:14 UTC

README

Latest Stable Version Latest Unstable Version Total Downloads PHP-Eye

StyleCI Build Status codecov License

A simple PHP API extension for DateTime. This is a modernized nesbot/carbon package. http://carbon.nesbot.com

use CarbonLocalized\Carbon;

printf("Right now is %s", Carbon::now()->toDateTimeString());
printf("Right now in Vancouver is %s", Carbon::now('America/Vancouver'));  //implicit __toString()
$tomorrow = Carbon::now()->addDay();
$lastWeek = Carbon::now()->subWeek();
$nextSummerOlympics = Carbon::createFromDate(2012)->addYears(4);

$officialDate = Carbon::now()->toRfc2822String();

$howOldAmI = Carbon::createFromDate(1975, 5, 21)->age;

$noonTodayLondonTime = Carbon::createFromTime(12, 0, 0, 'Europe/London');

$worldWillEnd = Carbon::createFromDate(2012, 12, 21, 'GMT');

// Don't really want to die so mock now
Carbon::setTestNow(Carbon::createFromDate(2000, 1, 1));

// comparisons are always done in UTC
if (Carbon::now()->gte($worldWillEnd)) {
    die();
}

// Phew! Return to normal behaviour
Carbon::setTestNow();

if (Carbon::now()->isWeekend()) {
    echo 'Party!';
}
echo Carbon::now()->subMinutes(2)->diffForHumans(); // '2 minutes ago'

// ... but also does 'from now', 'after' and 'before'
// rolling up to seconds, minutes, hours, days, months, years

$daysSinceEpoch = Carbon::createFromTimestamp(0)->diffInDays();

// Unfortunately the base class DateTime does not have any localization support.
// To begin localization support a formatLocalized($format) method was added.
// The implementation makes a call to date() using the current instance timestamp.
// If you first set the current locale with function Carbon\Carbon::setlocale()
// then the string returned will be formatted in the correct locale.
Carbon::setlocale('en');
echo Carbon::now()->formatLocalized('d M Y');    // 13 Jul 2016
echo Carbon::now()->formatLocalized('d F Y');    // 13 July 2016

Carbon::setlocale('ru');
echo Carbon::now()->formatLocalized('d M Y');    // 13 Июл 2016
echo Carbon::now()->formatLocalized('d F Y');    // 13 Июль 2016

// Documentation for other methods available on the website http://carbon.nesbot.com

Installation

With Composer

$ composer require andrey-helldar/carbon
{
    "require": {
        "andrey-helldar/carbon": "~1.21"
    }
}
<?php
require 'vendor/autoload.php';

use CarbonLocalized\Carbon;

printf("Now: %s", Carbon::now());

Without Composer

Why are you not using composer? Download Carbon.php from the repo and save the file into your project path somewhere.

<?php
require 'path/to/Carbon.php';

use CarbonLocalized\Carbon;

printf("Now: %s", Carbon::now());

Install to Laravel 5+

In the section aliases file config/app.php add:

'Carbon\Carbon' => \CarbonLocalized\Carbon::class,

This allows you to use a standard method of Carbon in the code.