How to convert a carbon into string, to take the date only?

How to convert a carbon into string, to take the date only?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Extracting Date Strings from Carbon Instances in Laravel

When working with Laravel models, the created_at timestamp is almost always handled by the Carbon library. While this provides powerful features for time zone manipulation and relative dates (like "2 days ago"), it can be a nuisance when you simply need to dump the raw date component into a string format like "YYYY-MM-DD". If you try to cast a Carbon instance directly to a string using standard PHP casting or strval(), you often get the full timestamp including hours, minutes, and seconds.

The most direct approach to isolate just the date portion is using Carbon's built-in format method. This method accepts a format string compatible with the date() function in PHP. To extract only the year, month, and day, you would pass 'Y-m-d' as the argument.

Here is how you would retrieve that specific value from the collection item shown in your example:

use Carbon\Carbon;

// Assuming $item is the array or model instance containing 'created_at'
$carbonInstance = $item['created_at'];

$dateOnlyString = $carbonInstance->format('Y-m-d');

dd($dateOnlyString); 
// Output: 2018-05-28

This approach is robust because it relies on Carbon's internal logic to handle the underlying DateTime object. It ensures that regardless of whether the original timestamp was stored with microseconds or in a specific UTC offset, the output string remains consistent based on your format mask. You can swap 'Y-m-d' for other formats like 'd.m.Y' or 'F j, Y' depending on the requirements of your API response or database export.

Alternatively, if you are working within an Eloquent query builder context and want to filter records by date rather than just displaying it, Carbon offers a copy() method combined with startOfDay() or similar helpers to manipulate the instance before querying. However, for simple string extraction, sticking to format('Y-m-d') is the most readable and performant solution.

One common pitfall when dealing with timestamps in Laravel involves time zones. The example data shows a timestamp of 2018-05-28 04:22:26.0 UTC. If your application's default timezone is set to something like 'America/New_York', simply casting the Carbon instance might shift the date if the conversion crosses midnight. While format() respects the Carbon instance's current timezone context, it does not automatically adjust the time unless you explicitly change the timezone using $carbonInstance->timezone('UTC') or $carbonInstance->setTimezone().

If you are building an API response where you need to guarantee a specific date format regardless of server configuration, consider defining a global helper function. This prevents developers from having to remember format() calls in every controller or job:

// In app/Helpers/DateHelper.php (or similar)
function formatDateOnly($carbonInstance): string
{
    return $carbonInstance->format('Y-m-d');
}

This abstraction keeps your controllers clean and ensures consistency across the codebase. It also makes it easier to switch date formats in the future without hunting through multiple files.

If you find yourself needing this conversion frequently for exporting data to CSV or JSON, remember that Carbon instances are serializable. You can dump the object directly using json_encode($item) if the serializer is configured to handle Carbon objects as ISO 8601 strings by default. However, in many cases, Laravel's default serialization converts Carbon to a string representation that includes microseconds and the timezone offset (C). In those scenarios, manual formatting via format() remains the necessary step to sanitize the output before transmission.

For more complex scenarios involving multiple date fields or dynamic formatting requirements, you might look into packages like laravel-carbon extensions, though the core functionality usually suffices for standard needs. Always verify the timezone configuration in your .env file (APP_TIMEZONE) if the extracted dates seem inconsistent across different environments, as this can silently alter the result of date calculations.

Our network