Warning: date_format() expects parameter 1 to be DateTime

Warning: date_format() expects parameter 1 to be DateTime

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

Stefan Bogdanescu

Founder & Senior Architect

Warning: date_format() expects parameter 1 to be DateTime

When you encounter the PHP warning date_format() expects parameter 1 to be DateTime, string given, it is almost always a type mismatch between what your function expects and what your variable actually holds. In the specific scenario described, the developer attempts to format a date retrieved directly from a MySQL database using date_format(). Despite the dates appearing correct within the database management interface, passing them straight to this function triggers an immediate failure because the underlying data type is not what the function requires.

The root cause lies in how PHP handles string parsing versus object instantiation. The date_format() function was designed to operate on objects of the DateTime class. It does not accept a raw string representing "2023-10-25" or any standard date format. When you execute $formattedweddingdate = date_format($weddingdate, 'd-m-Y');, PHP checks the type of $weddingdate. Since it is a string fetched from fetch_assoc(), the function throws a warning and returns false or an empty string rather than the formatted result.

To resolve this, you must explicitly convert the string into a DateTime object before formatting it. This conversion tells PHP to parse the date string according to its internal logic and create an object that represents that point in time. Once the object exists, you can safely pass it to date_format().

Here is how to correct the line within your script:

// Incorrect approach (triggers warning)
$formattedweddingdate = date_format($weddingdate, 'd-m-Y');

// Correct approach
$formattedweddingdate = date_format(new DateTime($weddingdate), 'd-m-Y');

By wrapping $weddingdate in new DateTime(), you create a valid object instance. This allows the subsequent date_format() call to execute as intended, converting the internal timestamp representation into the human-readable "d-m-Y" format you desire.

It is also worth noting that if your database column stores dates as Unix timestamps (integers) rather than strings like 'YYYY-MM-DD', the DateTime constructor still handles these seamlessly. However, if the data comes back as an integer and you attempt to use it directly with date_format() without conversion, you will face similar issues depending on how strict your PHP version is regarding type coercion in that specific context.

Beyond fixing the immediate error, this situation highlights a common pattern in legacy PHP development: assuming that raw strings behave like objects. Functions prefixed with date_ often have distinct overloads. For example, strtotime() accepts strings and returns a timestamp, while DateTime::createFromFormat() is strictly for parsing custom string patterns into objects. Confusing these interfaces leads to the exact error seen in the provided code snippet.

If you are building an application that requires robust date handling across multiple views or API endpoints, consider leveraging Laravel's Eloquent ORM if your stack allows it. Laravel provides helper methods like Carbon (via the carbon/laravel package) which handle these conversions automatically and offer a more expressive syntax for common operations:

use Carbon\Carbon;

// If using Laravel/Carbon
$formattedweddingdate = Carbon::parse($row2['weddingdate'])->format('d-m-Y');

This approach abstracts away the manual instantiation of DateTime objects, reducing the likelihood of type-related errors in larger applications. Regardless of whether you are sticking to vanilla PHP or migrating to a framework like LaravelCompany.com, understanding the distinction between string data and object instances is crucial for writing stable, warning-free code.

Our network