Last Updated:

Working with date and time in Laravel and PHP with Carbon

Hello. In this article, we'll take a look at the wonderful Carbon package that allows us to work with the date and time in PHP. This package is part of Laravel by default. Main functions of Carbon:

  • Working with time zones
  • Get the current time
  • Date addition and subtraction (e.g. +2 days or -3 weeks)
  • Etc.

Interesting? Then let's get started.

 

Carbon is a package for working with time and date in PHP. Official website of Carbon. Carbon Documentation

Connecting Carbon

So how I consider working with time in Laravel using Carbon. And Carbon by default comes as part of Laravel. So to start using it, we just need to import Carbon from the Carbon namespace:

<?php

use Carbon\Carbon;

Get the date and time

Get the current date and time

$date = Carbon::now();

Let's see what is contained in the $date using dd

date and time in Laravel 1

There is a second way to get the current date:

$current = new Carbon();

Get the current date

$date = Carbon::today();

Let's see the result:

date and time in Laravel 2

Get tomorrow's date.

$date = Carbon::tomorrow();

Get a specific date.

For example, we want to get the last Friday of this month.

$date = Carbon::now();

$lastFriday = new Carbon('last friday of '.$date);

Let's see the result:

date and time in Laravel 3

Create a date with clear control

We can also create a date from a certain number of arguments:

Carbon::createFromDate($year, $month, $day, $tz);

Or time:

Carbon::createFromTime($hour, $minute, $second, $tz);

Or time and date:

Carbon::create($year, $month, $day, $hour, $minute, $second, $tz);

And one small but main nuance, if one of these parameters is passed as null, then the current value will be substituted.

Date/Time Manipulation

Carbon also allows us to easily manipulate the date and time.

Add 3 days to the current date.

$current = Carbon::now();

$date = Carbon::now()->addDays(3);

Let's see the result. I'll take out the $current first, and then $date

date and time in Laravel 4

Adding one day

$date = Carbon::now()->addDay();

Subtract one day

$date = Carbon::now()->subDay();

Subtract three days

$date = Carbon::now()->subDays(3);

Add 5 years to the current date

$date = Carbon::now()->addYears(5);

Add one year

$date = Carbon::now()->addYear();

Subtract one year

$date = Carbon::now()->subYear();

Subtract five years

$date = Carbon::now()->subYears(5);

There are similar functions with the addition and subtraction of months

$date = Carbon::now()->addMonths(2);

$date = Carbon::now()->addMonth();

$date = Carbon::now()->subMonths(2);

$date = Carbon::now()->subMonth();

Similar to weeks (addition and subtraction of weeks)

$date = Carbon::now()->addWeeks(3);

$date = Carbon::now()->addWeek();

$date = Carbon::now()->subWeeks(3);

$date = Carbon::now()->subWeek(3);

There are also methods for working with hours, minutes, and seconds.

Relative time

Carbon contains very useful and powerful methods for displaying the time difference. For example:

$current = Carbon::now();

$date = Carbon::now();

$date->addHours(10);

echo $current->diffInHours($date, false);

Will output 10. And the following code will output -10.

$current = Carbon::now();

$date = Carbon::now();

$date->addHours(10);

echo $date ->diffInHours($current, false);

Also, there are methods to display the difference in minutes and days.

  • diffInMinutes
  • diffInDays

There are also special filtering features called diffInDaysFiltered(), diffInHoursFiltered(), and diffFiltered(), and diffFiltered() to help you filter the difference by day, hour, or custom interval. For example, to count the weekend between two instances. Which you can read about in the official documentation.

Conclusion.

The Carbon package allows us to significantly simplify and speed up the work with time and dates. The package has a wide range of possibilities for receiving and manipulating dates.