Greetings! I'm Aneesh Sreedharan, CEO of 2Hats Logic Solutions. At 2Hats Logic Solutions, we are dedicated to providing technical expertise and resolving your concerns in the world of technology. Our blog page serves as a resource where we share insights and experiences, offering valuable perspectives on your queries.
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 CarbonCarbon; for name space.
That’s it. We successfully integrated carbon required file. Now let’s check.
1 2 3 | require 'path/to/Carbon.php'; use CarbonCarbon; printf("Now: %s", Carbon::now()); |
FIRST STEP
Let’s start with checking current date and time. For that write following code in test file.
1 | echo Carbon::now(); |
EXAMPLE USESES
Initially create object carbon object from the class
1 | $carbon = new Carbon(); |
These following lines are a simple example to find out yesterday and tomorrow.
1 2 | 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.
1 | $carbon = new Carbon('first day of next week'); |
we have access to a smorgasbord of helper checkers and getters:
1 2 3 4 5 6 7 8 9 10 11 12 13 | $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:
1 | 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:
1 | 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-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | $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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | $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!