PHP provides a built-in DateTime class for dealing with the date and time. Using this class, you can deal with the date and time in your applications. It allows you to print different date formats, timestamps, DateTime in other time zones, etc. This default class works perfectly. But at the same time, it is a little bit difficult for beginners.
A more easy approach to working with the date and time in PHP is using the Carbon library. It’s an extension to the DateTime class and is widely popular among developers. This package is built on top of the DateTime class in PHP. In this tutorial, I’ll give a quick overview of using a Carbon library.
With the help of this library, one can perform all operations on the date and time that are allowed by the built-in PHP class. We’ll see a few of them in the next section.
Having said that, let’s first install the Carbon library. For installation, you should have Composer installed on your system. Open the terminal in your project root directory and run the command:
composer require nesbot/carbon
Working with DateTime Using Carbon
In order to use the Carbon package, you first need to include its environment in the application. You can do it using the code below:
<?php
require_once "vendor/autoload.php";
use Carbon\Carbon;
After this, you are free to play with all options provided by the library. Let’s see a few of them.
With Carbon, we can print the current date and time by the following statement.
echo Carbon::now();
PHP applications run on a server and each server has a default timezone. You’ll get the name of the default timezone as follows:
echo "Default timezone name: ". Carbon::now()->timezoneName;
In my case, I got the ‘Asia/Kolkata’ timezone. You may get a different timezone depending on your server settings.
If you wish to change the default timezone at runtime, you can easily change it as shown below.
$now = Carbon::now();
$now->timezone = new DateTimeZone('Europe/London');
Here, you must pass the correct name of the timezone. Click here to get the list of all available timezones.
The user can print the different date formats using Carbon. For instance, to print the formatted current date and time use the below statement.
echo "Formatted DateString: ". $now->format('l jS \\of F Y h:i:s A');
It gives the output of the current date and time as Sunday 10th of July 2022 05:38:25 PM
.
The Carbon library allows you to print current DateTime in any timezone. For example, to print the current time in London, the below code will be used.
$nowInLondonTz = Carbon::now('Europe/London');
echo "London Datetime now: ". $nowInLondonTz;
Print the current timestamp using the code:
$now = Carbon::now();
echo "Timestamp: ". $now->timestamp;
Addition and Subtraction(Days, Months, Years)
While working in a real-world application, you probably need to manipulate dates for different scenarios. In such cases, you may need to add or subtract days, months, and years to the current date. The Carbon package gives you an easy approach to manipulating dates.
Add Day(s)
$now = Carbon::now();
echo $now->addDay(); // add 1 day to current date
echo $now->addDays(10); // add 10 days
Subtract Day(s)
$now = Carbon::now();
echo $now->subDay(); // subtract 1 day from current date
echo $now->subDays(10); // subtract 10 days
Similarly, you can add or subtract months and years as follows.
// add month(s)
$now = Carbon::now();
echo $now->addMonth();
echo $now->addMonths(10);
// subtract month(s)
echo $now->subMonth();
echo $now->subMonths(10);
// add year(s)
echo $now->addYear();
echo $now->addYears(10);
// subtract year(s)
echo $now->subYear();
echo $now->subYears(10);
Apart from this, you can even add and subtract hours, minutes, seconds, quarters, weeks, centuries, etc. Read more about it in the documentation.
Compare Dates
Sometimes, you want to compare 2 dates in order to achieve certain functionality. With Carbon, you can compare dates as follows. Here, I am assigning 2 different dates to the variables $first
and $second
respectively, and comparing them.
$first = Carbon::create(2022, 07, 11);
$second = Carbon::create(2022, 07, 10);
var_dump($first->equalTo($second)); // false
var_dump($first->greaterThan($second)); // true
var_dump($first->lessThan($second)); // false
These are some basic examples of using the Carbon library. I hope you understand and can now easily integrate PHP DateTime with this package on your website.
Related Articles
- How to Integrate Google Sheets API with PHP
- Set Timezone in PHP By Editing php.ini File
- How to Handle Timezones in PHP
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
this site is very helpful for any web developers . in this site i can’t find how to show local date using carbon . i want to show ( thusday , 1st march 2018 , 12.45 am ) . so what can i do ???
Use below code and replace the timezone with your actual timezone.
$now = Carbon::now();
$now->timezone = new DateTimeZone(‘Europe/London’);
echo $now->format(‘l, jS M Y, g:i a’);