CARBON – a PHP Based Date and Time Library

PHP developers often have to spend lots of time trying to manipulate and format time and date.  Especially in applications where date and time calculations are used extensively, like an Event calendar, or a scheduling system which works across different timezones. There are different php libraries available to this. In this article let’s talk about Carbon php based date and time library. Which we are quite impressed about with the use in a recent Laravel project.

WHAT WE NEED

The only thing that we need is Carbon library. Download it from github , unzip and copy that folder to your project library folder.  Or Use the following command to install with composer.

$ composer require nesbot/carbon

 

INTEGRATION

·         Create a test file

·         Incude carbon.php file from source folder

·         use Carbon\Carbon; for name space.

That’s it. We successfully integrated carbon required file. Now let’s check.

 

require 'path/to/Carbon.php';
use Carbon\Carbon;
printf("Now: %s", Carbon::now());

FIRST STEP

Let’s start with checking current date and time. For that write following code in test file.

echo Carbon::now();

EXAMPLE USESES

Initially create object carbon object from the class

$carbon = new Carbon();

These following lines are a simple example to find out yesterday and tomorrow.

echo $carbon->tomorrow();
echo $carbon->yesterday();

The easiest way to get started with Carbon is to just pass a human readable date string into its constructor, along with an optional timezone.

 

$carbon = new Carbon('first day of next week');

we have access to a smorgasbord of helper checkers and getters:

 

$carbon->isWeekend();

$carbon->isFuture();

$carbon->isLeapYear(); 

$carbon->year;

$carbon->month; 

$carbon->daysInMonth;

$carbon->weekOfYear;

 

LOCALIZATION

Localization is also supported in carbon, so that we get output in any language installed on the machine. Note that we need to install the necessary locales for this to work.

To localize date and time strings, the standard PHP function setlocale() can be used:

setlocale(LC_TIME, 'German');echo $dt->formatLocalized('%A %d %B %Y');          // Mittwoch 21 Mai 1975

To localize the diffForHumans method the class offers its own setLocale method:

Carbon::setLocale('de');echo Carbon::now()->addYear()->diffForHumans();    // in 1 Jahr

GET PARTICULAR PART FROM DATE

if we want to get any particular part from date like year or day or month, we can write simple like follow-

$date = Carbon::create(2016, 6, 12, 23, 26, 11);

echo($date->month);

echo($date ->year);                                         

echo($date ->month);                                      

echo($date ->day);                                         

echo($date ->hour);                                        

echo($date ->minute);                                      

echo($date ->second);                                      

echo($date ->dayOfWeek);                               

echo($date ->dayOfYear);                                 

echo($date ->weekOfMonth);                           

echo($date ->weekOfYear);                              

echo($date ->daysInMonth);                             

echo($date ->timestamp);

 

FORMATTING

If we want to convert  date and time from one format to another format. In Carbon, it is very simple.

 

$dt = Carbon::create(2016, 6, 12, 14, 15, 16);



var_dump($dt->toDateTimeString() == $dt);          // bool(true) => uses __toString()

echo $dt->toDateString();                          // 2016-06-12

echo $dt->toFormattedDateString();                 // Jun 12, 2016

echo $dt->toTimeString();                          // 14:15:16

echo $dt->toDateTimeString();                      // 2016-06-12 14:15:16

echo $dt->toDayDateTimeString();                   // Sun, Jun 12, 2016 2:15 PM

DATE/TIME ADDITION AND SUBTRACTION

 

Sometimes, it is also needed to add some values with particularly year or month or day. In this case, we can use following methods.

$dt = Carbon::create(2012, 1, 31, 0);



echo $dt->toDateTimeString();            // 2012-01-31 00:00:00

echo $dt->addYears(5);                   // 2017-01-31 00:00:00

echo $dt->addYear();                     // 2018-01-31 00:00:00

echo $dt->subYear();                     // 2017-01-31 00:00:00

echo $dt->subYears(5);                   // 2012-01-31 00:00:00



echo $dt->addMonths(60);                 // 2017-01-31 00:00:00

echo $dt->addMonth();                    // 2017-03-03 00:00:00

echo $dt->subMonth();                    // 2017-02-03 00:00:00

echo $dt->subMonths(60);                 // 2012-02-03 00:00:00



echo $dt->addDays(29);                   // 2012-03-03 00:00:00

echo $dt->addDay();                      // 2012-03-04 00:00:00

echo $dt->subDay();                      // 2012-03-03 00:00:00

echo $dt->subDays(29);                   // 2012-02-03 00:00:00





echo $dt->addWeekdays(4);                // 2012-02-09 00:00:00

echo $dt->addWeekday();                  // 2012-02-10 00:00:00

echo $dt->subWeekday();                  // 2012-02-09 00:00:00

echo $dt->subWeekdays(4);                // 2012-02-03 00:00:00





echo $dt->addWeeks(3);                   // 2012-02-24 00:00:00

echo $dt->addWeek();                     // 2012-03-02 00:00:00

echo $dt->subWeek();                     // 2012-02-24 00:00:00

echo $dt->subWeeks(3);                   // 2012-02-03 00:00:00





echo $dt->addHours(24);                  // 2012-02-04 00:00:00

echo $dt->addHour();                     // 2012-02-04 01:00:00

echo $dt->subHour();                     // 2012-02-04 00:00:00

echo $dt->subHours(24);                  // 2012-02-03 00:00:00





echo $dt->addMinutes(61);                // 2012-02-03 01:01:00

echo $dt->addMinute();                   // 2012-02-03 01:02:00

echo $dt->subMinute();                   // 2012-02-03 01:01:00

echo $dt->subMinutes(61);                // 2012-02-03 00:00:00

 

There are more methods that  can use in our application for managing date and time. You can find them here  Carbon docs

Happy coding!