Use Carbon PHP to format a date

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering Date Formatting in PHP with Carbon: Solving Your ISO String Challenge

As developers working with databases and APIs, dealing with date and time manipulation is one of the most frequent tasks. When you pull a timestamp from a database, it often arrives in a standardized format, like the ISO 8601 string you provided: 2017-02-06T22:25:12Z. The challenge then becomes transforming this raw data into a human-readable, specific format.

Today, we are diving deep into how to use Carbon, the powerful date and time library built on top of PHP's DateTime class, to correctly format these strings. We will analyze why your initial attempt didn't work and provide the definitive, best-practice solution.

Why Initial Formatting Attempts Fail

You tried using Carbon::createFromFormat('Y-m-d H', '2017-02-06T22:25:12Z') followed by toDateTimeString(). This approach often encounters issues because:

  1. Parsing Ambiguity: While createFromFormat is excellent for parsing non-standard strings, the input string you provided (2017-02-06T22:25:12Z) contains extra characters (T and Z) that confuse the strict format you provided (Y-m-d H).
  2. Conversion vs. Formatting: Methods like toDateTimeString() are designed to return a standard database or ISO string representation, not a custom display string. To achieve a specific visual layout (like 06-02-17 22:25:12), you need to use Carbon's dedicated formatting functions.

The Correct Approach with Carbon

The most robust and simplest way to handle this is to let Carbon parse the input string directly, as it is highly optimized for ISO 8601 standards. Once the date object is created, we use the powerful format() method to dictate the exact output structure we require.

Here is the correct implementation to achieve your desired format: 06-02-17 22:25:12.

Code Example

<?php

use Carbon\Carbon;

// The raw date received from the database
$dateTimeString = '2017-02-06T22:25:12Z';

// 1. Create a Carbon instance by parsing the ISO string directly
$dateObject = Carbon::parse($dateTimeString);

// 2. Format the object into the desired custom string format
// d = day (01-31)
// m = month (01-12)
// y = year (1901-2100)
// H = hour (24-hour format)
// i = minutes
// s = seconds
$formattedDate = $dateObject->format('d-m-y H:i:s');

echo "Original Date String: " . $dateTimeString . "\n";
echo "Formatted Date: " . $formattedDate . "\n";

// Example output verification: 06-02-17 22:25:12

Explanation of the Format Codes

The magic lies in understanding Carbon's format characters. By using ->format('d-m-y H:i:s'), we instruct Carbon to replace those placeholders with the corresponding numerical values from the date object, separated by the literal characters you specify (-, space, :).

  • d: Day of the month, 2 digits with leading zero.
  • m: Month, 2 digits with leading zero.
  • y: Year, 4 digits.
  • H: Hour in 24-hour format.
  • i: Minutes.
  • s: Seconds.

This method is far superior because it handles time zone awareness implicitly and ensures the resulting string matches your exact presentation requirements, regardless of the original input structure. For complex date manipulations within a Laravel application, relying on well-tested libraries like Carbon ensures consistency across your entire codebase, mirroring the robust ecosystem provided by the Laravel Company.

Conclusion

Dealing with dates is often where developers encounter subtle bugs due to inconsistent parsing or formatting rules. By shifting from complex manual creation methods to leveraging Carbon's intuitive parse() and format() methods, you can write cleaner, more readable, and significantly more reliable date handling logic. Always favor these high-level methods when working with Carbon; they abstract away the complexity of time zones and locale settings, allowing you to focus on the business logic of your application.