Create Carbon date with string
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Title: Create Carbon Date Object from String for Decremental Pagination
Introduction
Carbon is a great PHP library that provides powerful date manipulation functions. One of its useful features includes the creation of date objects from predefined format strings. In this article, we'll discuss how to create a Carbon date object from a string representing a past date and use it in our code for decremental pagination. By following these steps, you can successfully show dates in descending order while paginating through them.
Step 1: Create the Date Object from String
First, extract the given date string (in this case '19/02/2018 00:00:00') and store it as a constant or variable for easier access later in our code. We can use the Carbon::createFromFormat() function to create an instance of the Carbon date object from this string.
Example Code (with a hard-coded date):
```php
$today = Carbon::now(); // Assuming we want today's date as our starting point
// Alternatively, use $today = Carbon::createFromFormat('d-m-Y H:i:s', '19/02/2018 00:00:00');
```
Step 2: Iterate through Decremented Days with a Loop
Next, we need to iterate through the days leading up to our starting date. Start by initializing an empty array (or any other data structure) for storing these decremented dates. Then, create a loop that subtracts one day from our starting date each iteration and formats it in the desired format ('d/m/Y'). Finally, append this formatted date to the array of last days.
Example Code:
```php
$lastDays = array();
for ($i = 1; $i < 10; $i++) {
$day = $today->subDays(1)->format('d/m/Y'); // Format the decremented date
$lastDays[] = $day; // Add it to our array of last days
}
```
Step 3: Handle Errors and Ensure Correct Inputs
In some cases, attempting to create a Carbon object from an invalid string may cause errors. You should include proper error handling and checks for your date strings to ensure consistent execution of your pagination logic. For instance, you can use try/catch blocks with appropriate exceptions or assertions on the given input. This will help maintain robust code that handles all possible scenarios gracefully.
Conclusion
By following these steps, you're now able to successfully create a Carbon date object from a string and manipulate it for decremental pagination in your application. Always remember to handle errors and use proper input validation to ensure reliable functionality across different scenarios. For additional resources on working with dates using the Laravel Carbon library, refer to https://laravelcompany.com/blog/using-carbon-date-time-manipulation-in-your-laravel-applications.