Getting only time from Carbon object

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# Mastering Time Extraction: Getting Only Time from Carbon Objects in Data Processing Working with external data sources, such as Excel files, often introduces complexity when dealing with date and time objects. When you import data, you frequently receive full timestamps, but sometimes you only need the time component. For developers leveraging powerful libraries like Carbon, the challenge lies in correctly parsing disparate string formats into meaningful, manipulable objects. This post dives deep into a common scenario: how to reliably extract *only* the time (e.g., `01:45:00`) from a Carbon object derived from raw data, avoiding errors related to incorrect formatting or null values. ## The Challenge: Parsing Time Strings from Excel Imagine reading an Excel file where a column contains timestamps formatted like `"1:45:00"`. When you try to convert this into a Carbon object using standard parsing methods, issues arise, especially when mixing date and time components. The initial attempt often looks something like this: ```php // Example of the initial confusion $timeString = "1:45:00"; // Attempting to create a full date/time object from just a time string $carbonTime = Carbon::createFromFormat('Y-m-d h:i:s', $timeString); // This often fails or produces unexpected results. ``` The errors you encountered, such as `Trying to get property 'date' of non-object` or `Call to a member function format() on null`, stem from trying to apply date formatting methods to an object that either failed to instantiate correctly (resulting in `null`) or was not the expected Carbon instance. This indicates a mismatch between the input string and the strict parsing rules of the Carbon methods. ## The Solution: Targeted Parsing and Extraction The key to solving this is to adjust how you parse the data based on what you actually possess. If your Excel column *only* contains time (HH:MM:SS), you should avoid trying to force it into a full date structure unless you have a corresponding date reference. ### Strategy 1: Parsing Time Strings Directly If the input string is purely a time format, we can leverage Carbon's ability to handle time-only strings more flexibly. However, since Excel often mixes dates and times, a safer approach is to isolate the time component *after* successfully creating the initial object, or use specialized parsing for time-only inputs. A robust approach involves ensuring you are only attempting to parse the relevant parts, or using methods designed specifically for time manipulation. Here is how you can refine your data reading process: ```php use Carbon\Carbon; //