How to Work With PHP DateTime Using Carbon

PHP provides built-in DateTime class which is used for dealing with date and time. Using this class, we can print different date formats, timestamps, DateTime in different time zones. This default class works perfectly. But at the same time, it is a little bit difficult for beginners.

If someone is looking for a more easy approach to working with date and time in PHP, then Carbon library is the right choice. The library provides detailed documentation about integration. In this tutorial, we will give a quick overview of using a Carbon library.

Basically, Carbon is an API extension for DateTime. This library is built on the top of the DateTime class of PHP.

Using this library one can perform all operations on DateTime which is allowed by this built-in PHP class.

Having said that, let’s first install the Carbon library.

You should have Composer installed on your system for the library installation. Open the terminal in your project root directory and run the command below:

composer require nesbot/carbon

Working With DateTime Using Carbon

In order to use the Carbon library, we first need to include their environment in the application. You can include its environment 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 a library. Let’s see some of them.

PHP applications run on a server and each server has a default timezone. We can get the name of the default timezone as follows:

<?php
require_once "vendor/autoload.php";
use Carbon\Carbon;

echo "Default timezone name: ". Carbon::now()->timezoneName;
?>

In my case, I got the timezone ‘Asia/Kolkata’. You may get a different timezone name depending on your server settings.

If you want to change the default timezone for your application then you can set it easily as shown below.

$now = Carbon::now();
$now->timezone = new DateTimeZone('Europe/London');

Click here to get the list of all available timezones.

We can print different date formats using Carbon. For instance, let’s print the current date and time.

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 25th of October 2020 05:38:25 PM.

You can print current DateTime in any timezone. To print the current time in London, our code will be:

$nowInLondonTz = Carbon::now('Europe/London');
echo "London Datetime now: ". $nowInLondonTz;

Print the current timestamps using the code:

$now = Carbon::now();
echo "Timestamp: ". $now->timestamp;

These are basic examples of using the Carbon library. You can explore more about this from their official documentation.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

2 thoughts on “How to Work With PHP DateTime Using Carbon

  1. 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 ???

    1. 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’);

Leave a Reply

Your email address will not be published. Required fields are marked *