Laravel – Carbon and Queries

Laravel

Carbon is probably the most effective way to handle DateTime strings flawlessly and implement PHP code that everybody can easily extend or debug.

In the following example, I would like to demonstrate how you can filter a collection in Laravel by using Carbon. As you can see, the below example will simply return all People who have registered in the past two weeks.

The method subWeeks() will do all the work for you and return a valid DateTime string from 14 days ago which can be used for the whereBetween query method. If you prefer using days, you can simply use subDays() instead.

One possible use case could be a front-end controller that handles AJAX requests.