Easier Date/Time in Laravel and PHP with Carbon
Easier Date/Time in Laravel and PHP with Carbon
While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.
Introduction
Working with date and time in PHP is not the easiest or most clear of tasks. We have to deal with strtotime
, formatting issues, lots of calculations, and more.
The nifty package called Carbon can help make dealing with date/time in PHP much easier and more semantic so that our code can become more readable and maintainable.
Carbon is a package by Brian Nesbit that extends PHP’s own DateTime class.
It provides some nice functionality to deal with dates in PHP. Specifically things like:
- Dealing with timezones
- Getting current time easily
- Converting a datetime into something readable
- Parse an English phrase into datetime (first day of January 2016)
- Add and Subtract dates (+ 2 weeks, -6 months)
- Semantic way of dealing with dates
All of the above lead to a very useful package that makes it a breeze to deal with times in PHP.
Setup
In order to use Carbon, you’ll need to import Carbon from the Carbon
namespace. Luckily for us, Carbon is already included in Laravel so there’s no need to go and add it with Composer.
Whenever we need to use Carbon, we can import it like so:
After importing, let’s look at some cool things we can do with this great package.
Getting a Specific Date/Time
Creating Dates with More Fine Grained Control
In addition to the quick ways to define date/times, Carbon also let’s us create date/times from a specific number of arguments.
These are very helpful when you get some sort of date or time in a format that isn’t normally recognized by Carbon. If you pass in null
for any of those attributes, it will default to current.
Manipulating the Date/Time
Grabbing the date/time isn’t the only thing you’ll need to do when working with dates. You’ll often need to manipulate the date or time.
For instance, when creating a trial period for a user, you will want the trial period to expire after a certain amount of time. So let’s say we have a 30 day trial period. We could easily calculate that time with add
and subtract
.
For this trial period, we would do:
From the Carbon docs, here are some of the other add()
and sub()
methods available to us:
Getters and Setters
Another quick way to manipulate or read the time is to use the getters and setters available.
We can even string together some setters
Formatting
In that example above, you may have noticed the ->toDateTimeString()
method. We can easily format the date/time for our purposes. In that case, we got a datetime string.
Relative Time
Carbon lets us easily display time relatively with the diff()
methods.
For instance, let’s say we have a blog and wanted to show a published time of 3 hours ago. We would be able to do that with these methods.
Finding the Difference
These methods are used to just find the number of difference.
Displaying the Difference for Humans
Displaying time relatively can sometimes be more useful to readers than a date or timestamp.
For example, instead of displaying the time of a post like 8:12am, the time will be displayed as 3 hrs ago.
The diffForHumans()
method is used for calculating the difference and also converting it to a humanly readable format.
Here are some examples:
Conclusion
There’s plenty more that Carbon can do. Be sure to look through the official Carbon docs. Hopefully this helps use date/times easier in PHP and speeds up development times!
Easier Date/Time in Laravel and PHP with Carbon
While this tutorial has content that we believe is of great benefit to our community, we have not yet tested or edited it to ensure you have an error-free learning experience. It's on our list, and we're working on it! You can help us out by using the "report an issue" button at the bottom of the tutorial.
Introduction
Working with date and time in PHP is not the easiest or most clear of tasks. We have to deal with strtotime
, formatting issues, lots of calculations, and more.
The nifty package called Carbon can help make dealing with date/time in PHP much easier and more semantic so that our code can become more readable and maintainable.
Carbon is a package by Brian Nesbit that extends PHP’s own DateTime class.
It provides some nice functionality to deal with dates in PHP. Specifically things like:
- Dealing with timezones
- Getting current time easily
- Converting a datetime into something readable
- Parse an English phrase into datetime (first day of January 2016)
- Add and Subtract dates (+ 2 weeks, -6 months)
- Semantic way of dealing with dates
All of the above lead to a very useful package that makes it a breeze to deal with times in PHP.
Setup
In order to use Carbon, you’ll need to import Carbon from the Carbon
namespace. Luckily for us, Carbon is already included in Laravel so there’s no need to go and add it with Composer.
Whenever we need to use Carbon, we can import it like so:
After importing, let’s look at some cool things we can do with this great package.
Getting a Specific Date/Time
Creating Dates with More Fine Grained Control
In addition to the quick ways to define date/times, Carbon also let’s us create date/times from a specific number of arguments.
These are very helpful when you get some sort of date or time in a format that isn’t normally recognized by Carbon. If you pass in null
for any of those attributes, it will default to current.
Manipulating the Date/Time
Grabbing the date/time isn’t the only thing you’ll need to do when working with dates. You’ll often need to manipulate the date or time.
For instance, when creating a trial period for a user, you will want the trial period to expire after a certain amount of time. So let’s say we have a 30 day trial period. We could easily calculate that time with add
and subtract
.
For this trial period, we would do:
From the Carbon docs, here are some of the other add()
and sub()
methods available to us:
Getters and Setters
Another quick way to manipulate or read the time is to use the getters and setters available.
We can even string together some setters
Formatting
In that example above, you may have noticed the ->toDateTimeString()
method. We can easily format the date/time for our purposes. In that case, we got a datetime string.
Relative Time
Carbon lets us easily display time relatively with the diff()
methods.
For instance, let’s say we have a blog and wanted to show a published time of 3 hours ago. We would be able to do that with these methods.
Finding the Difference
These methods are used to just find the number of difference.
Displaying the Difference for Humans
Displaying time relatively can sometimes be more useful to readers than a date or timestamp.
For example, instead of displaying the time of a post like 8:12am, the time will be displayed as 3 hrs ago.
The diffForHumans()
method is used for calculating the difference and also converting it to a humanly readable format.
Here are some examples:
Conclusion
There’s plenty more that Carbon can do. Be sure to look through the official Carbon docs. Hopefully this helps use date/times easier in PHP and speeds up development times!