How to get the only hours and minutes in laravel blade?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Mastering Time Display in Laravel Blade: Getting Hours, Minutes, and Seconds
Welcome to the world of Laravel development! As you start working with dates and times in Blade templates, it’s very common to run into questions about how to format these values correctly. Dealing with datetimes can be tricky, especially when you are new to object-oriented concepts like Carbon, which powers date management in Laravel.
You asked how to get only the hours, minutes, and seconds from a date, and your attempt using functions like `todatestring()` suggests you might be looking for a specific formatting method. As a senior developer, I can tell you that while many PHP functions exist, the most robust and idiomatic way to handle this in a Laravel application is by leveraging the powerful **Carbon** library.
This guide will walk you through the correct, clean, and professional way to extract and display time components in your Blade files.
## Why Direct Subtraction Fails
When you try to perform arithmetic operations directly on date objects (e.g., `$calllog['delivery_date'] - 1`), you are usually dealing with date *objects* or strings, not easily accessible time units. Trying to subtract an integer from a full date object rarely yields the simple hour/minute/second breakdown you need for display.
The key is recognizing that Laravel heavily relies on Carbon to handle these complexities. Carbon makes date manipulation intuitive and safe.
## The Correct Approach: Using Carbon's `format()` Method
To extract specific parts of a datetime, you should use the `format()` method provided by the Carbon instance. This method allows you to specify exactly how you want the date or time string to appear.
If your `$calllog['delivery_date']` variable is a Carbon instance (which it usually is when retrieved from a database via Eloquent models), you can extract the hours, minutes, and seconds directly.
Here is the step-by-step process:
### Example 1: Formatting to HH:MM:SS
To get the standard 24-hour time format with hours, minutes, and seconds, use the appropriate Carbon format characters:
```php
{{ $calllog['delivery_date']->format('H:i:s') }}
```
**Explanation:**
* `->format()` is the method used to apply a specific string pattern to the date object.
* `H` represents the 24-hour format of an hour (00 to 23).
* `i` represents the minutes (00 to 59).
* `s` represents the seconds (00 to 59).
### Example 2: Customizing the Output
You can also customize the output further. For instance, if you want leading zeros for single-digit numbers (e.g., showing `09:05:01` instead of `9:5:1`), the format above is perfect.
If you wanted a more descriptive string, you could combine it with the date itself:
```php
Delivery Time: {{ $calllog['delivery_date']->format('h:i:s A') }}
``` In this example, `A` will display AM or PM, making the output much more readable. ## Best Practices for Date Handling in Laravel When working with dates and times in Laravel, always remember these best practices: 1. **Always Use Carbon:** Never try to manipulate raw PHP `DateTime` objects directly unless you fully understand their nuances. Always ensure your date objects are instances of Carbon, which is seamlessly integrated into the Laravel ecosystem. For more complex operations, exploring how data flows through Eloquent models and Carbon provides a solid foundation for building robust applications, much like the principles discussed at [https://laravelcompany.com](https://laravelcompany.com). 2. **Timezones Matter:** Be mindful of timezones. By default, Carbon often uses the timezone set in your application configuration. If you are dealing with international data, ensure your database stores times in UTC and convert them to the user's local timezone only right before displaying them in Blade. 3. **Use Eloquent Casting:** If you are fetching dates from a database, configure your Eloquent models to cast these fields directly to Carbon objects. This ensures that when you access `$model->date`, you are immediately working with a powerful date object, simplifying all future formatting tasks. ## Conclusion Getting the hours, minutes, and seconds in Laravel Blade is straightforward once you embrace the right tools. Forget custom functions; rely on Carbon's built-in `format()` method. By using methods like `$date->format('H:i:s')`, you ensure your code is readable, maintainable, and accurate, giving you a solid foundation for building fantastic Laravel applications. Happy coding!