Carbon: displaying hours and minutes?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Displaying Hours, Minutes, and Seconds with Carbon in Laravel Views Body: When working on a web application development project, it is essential to provide clear and comprehensive information about upcoming dates, events, or deadlines. The built-in Carbon library within Laravel provides an effective way to handle date manipulation and formatting. However, you may have encountered some difficulties in displaying the different components of time, such as hours and minutes, alongside days. This guide will walk you through how to adjust your code for a more comprehensive display using Carbon. To begin with, we need to understand how Carbon's `diffForHumans()` method works. It accepts a Carbon object representing two dates: the current date and another future date. The function then calculates the difference between these two dates and returns a human-readable format. The default output shows only the days, as illustrated in your example (6 days). If you want to display hours and minutes along with days, we need to modify this approach slightly. The first step is to create two Carbon objects: one representing the current date and another for the future date. Here's an updated code snippet: ```php $currentDate = \Carbon\Carbon::now(); $futureDate = \Carbon\Carbon::createFromTimeStamp(strtotime($item->created_at)); ``` Next, we need a `diffInMinutes()` method to calculate the time difference between these two dates. Since Carbon doesn't provide this directly, we can create our custom function to handle this requirement: ```php function diffInMinutes($date1, $date2) { return \Carbon\Carbon::parse($date2)->diffInMinutes(\Carbon\Carbon::parse($date1)); } ``` Now that we have the time difference in minutes, let's create a new variable to store this result: ```php $timeDifferenceMinutes = diffInMinutes($currentDate, $futureDate); ``` To display the formatted time including hours and minutes alongside days, we can use `diffForHumans()` with an additional format string. We will add a new format string called 'P' which stands for precision. This allows us to control how many digits are displayed after the decimal point. By default, it is set to 2; however, you might want to adjust this depending on your needs: ```php $formattedTime = \Carbon\Carbon::now()->diffForHumans($futureDate, null, null, true)->format('%d days %P%h:%M %p'); ``` In this example, we have modified the format string to include `'%P%h:%M %p'`, which will display the hours and minutes in a human-readable fashion. If you wish to add seconds as well, simply modify it to: ```php $formattedTime = \Carbon\Carbon::now()->diffForHumans($futureDate, null, null, true)->format('%d days %P%h:%M:%s %p'); ``` This code snippet will show the full time difference including days, hours, minutes, and seconds if needed. As you can see, by using Carbon's powerful methods and a few modifications to your initial example, it is possible to create a more comprehensive output for human-readable dates in Laravel views. To conclude, we have provided a thorough answer with code examples that show how you can use Carbon library effectively to display the complete time difference, including days, hours, minutes, and seconds in Laravel 5.3 view. We encourage you to incorporate these adjustments and enhance your web application's user experience by offering more detailed information about upcoming events or deadlines. For further exploration on this topic, feel free to explore the Carbon library documentation at https://laravelcompany.com/products/carbon as well.