When writing PHP applications there’s often a need for working with dates and times. In this tutorial, I’ll be walking you through Carbon, a simple PHP API extension for the DateTime class. It basically makes working with dates and times a lot more easier. I’ve personally used this library on my Ahead project, a Laravel app for scheduling posts to social media sites like Facebook, Twitter and LinkedIn.
To install Carbon, you need to use Composer.
Include the vendor autoload in your file and then use the
Carbon class in the
1 2 3 4 5
Getting the Current Date and Time
You can get the current date and time using the
now method. If you do not specify an argument, it uses the default timezone from your PHP config.
1 2 3
If you want to use a different timezone, you can pass a valid timezone as the argument.
1 2 3
Aside from the
now method, there’s also
today, which returns the start of today’s date,
yesterday, which returns the start of the date yesterday, and
tomorrow, which returns the start of the date tomorrow.
1 2 3 4 5
Converting to String
By default, methods in Carbon returns a datetime object. Though its an object, you can directly output it using
echo because of the
__toString magic method. But if you want to convert it to a string, you can use the
1 2 3 4
You can also parse dates in all sorts of formats using the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
You can also construct dates by using individual parts such as the year month and day.
1 2 3 4 5 6 7 8 9 10 11 12 13
Optionally, you can pass in any valid timezone as the last argument for the
Manipulating dates can be done by using
add (to add) or
sub (to subtract) followed by the unit of time you want to add or subtract. For example, you want to add a specific number of days to the specific date, you use the
addDays method. There’s also the
modify method which allows you to pass in the modification that you want. It takes the format of
- followed by the unit value and the unit. So if you want to add a year to the current date, you use
+1 year. Check out the examples below to have an idea what else you can do.
1 2 3 4 5 6 7 8 9
Commonly in PHP apps are international. This means that you need to work with different timezones. Here’s how to convert from one timezone to another.
1 2 3
You can use the following methods to compare dates in Carbon.
min– returns the smaller date.
max– returns the larger date.
eq– check if two dates are the same.
gt– check if the first date is greater than the second date.
lt– check if the first date is less than the second date.
gte– check if the first date is greater than or equal to the second date.
lte– check if the first date is lesser than or equal to the second date.
Other helpful methods include
If you’re on Laravel, you can just add an alias for the Carbon library in the
aliases in the
That’s it! In this tutorial, you’ve learned how to work with the Carbon library in PHP. Carbon is a really nice and easy way to work with dates in PHP. Be sure to check out the official docs to learn more about it.