Extracting Time Strings from Carbon Objects Without Losing Precision
When importing data from Excel files, you often encounter cells that contain purely temporal values rather than full timestamps. If your source data looks like 1:45:00 or 01:45:00, but the parsing logic wraps it in a Carbon instance that defaults to the current date (e.g., 2018-10-30 01:45:00), blindly inserting that object into a database will corrupt your records with unwanted dates. You need to isolate the time component before storage.
The core issue arises when you attempt to extract properties from a Carbon object incorrectly. In your specific case, you tried using $value["time"]->date->format("H:i:s"). This fails with "Trying to get property 'date' of non-object" because Carbon instances do not expose a public $date property in the way you might expect. The internal [date] key visible via print_r() is part of the object's state, not a public property accessible via dot notation.
To fix this, you must use the Carbon API methods designed for formatting. Once you have a valid Carbon instance representing your time value, calling the format() method on the root object is the correct approach. However, based on your second code snippet, it appears your initial parsing logic is actually returning null.
The line $time = Carbon::createFromFormat('Y-m-d h:i:s',$value["time"])->format('h:i:s'); is problematic for two reasons. First, your Excel column contains only the time string (e.g., "01:45:00"), not a formatted date like "2018-10-30 01:45:00". Passing a bare time string to createFromFormat expecting a full timestamp will fail to parse, resulting in null. Second, you are mixing case sensitivity; your Excel data shows 01:45:00, which implies a 24-hour format.
If you want to preserve the exact string representation from the Excel cell without forcing Carbon to re-parse it into a datetime object and then re-format it, you should rely on the raw string value if it hasn't been corrupted by the importer yet. However, if your importer has already converted that string into a Carbon object with an attached date (as shown in your print_r output), simply casting the object back to a string or formatting it requires care.
The most robust solution involves creating a Carbon instance from the time string using a format that matches the data exclusively. If your Excel column contains 01:45:00, you should use 'H:i:s' as the format string, not 'Y-m-d H:i:s'. This tells Carbon to treat the input purely as a time component.
Here is how you can adjust your loop:
foreach ($data as $key => $value) {
// Parse only the time component using the matching format
$timeInstance = Carbon::createFromFormat('H:i:s', trim($value["time"]));
if (!$timeInstance) {
continue; // Handle parsing errors gracefully
}
// Format back to string for storage, or keep as is if it matches desired output
$cleanTime = $timeInstance->format('H:i:s');
// Now $cleanTime contains "01:45:00" without the date prefix
}
Alternatively, if your goal is simply to store the value in a database column that expects a TIME datatype, you do not need to format it as a string at all. You can pass the Carbon instance directly to the database query builder or Eloquent model. Laravel's underlying drivers (like MySQL) will correctly interpret a Carbon object containing only time information when the target column is defined as time. However, if your Carbon object has been tainted with a default date (like 2018-10-30), you must explicitly set the date to null or adjust the instance before saving.
You can normalize the Carbon instance using copy() and setting the date to midnight of the current day, or simply by formatting it into a string that the database driver accepts as a pure time value. If you are using Laravel Company's resources for bulk operations, ensure your data mapping logic respects the schema of your target table. If the destination column is strictly TIME, injecting a full timestamp will result in type errors or incorrect data ingestion. Always validate that the input format matches the parsing expectations before committing to storage.