date_diff() expects parameter 1 to be DateTimeInterface, string given

date_diff() expects parameter 1 to be DateTimeInterface, string given

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

Stefan Bogdanescu

Founder & Senior Architect

The DateTimeInterface Requirement in PHP 8+

The error date_diff() expects parameter 1 to be DateTimeInterface, string given is a direct consequence of how PHP handles type safety when calculating time deltas. If you are running code against PHP 8 or later, the strict typing rules enforced by the engine mean that passing a raw string directly into date_diff() will fail immediately. The function signature explicitly requires instances of DateTimeInterface, not strings.

The source scenario illustrates this perfectly:

$date_expire = '2014-08-06 00:00:00';
$date1 = date("Y-m-d G:i:s");
$date2 = date_create($date_expire); // This works fine

$diff = date_diff($date1, $date2); // This line makes error.

In the example above, $date2 is correctly instantiated as a DateTime object using date_create(). However, $date1 is generated using the date() function. While date() returns a formatted string (e.g., "2014-08-06 00:00:00"), it does not return an object. When you pass that string alongside the $date2 object to date_diff(), PHP attempts to match the expected types. Since the first argument is a string, the function throws the fatal error regarding DateTimeInterface.

Resolving Type Mismatch

To fix this, you must ensure both arguments passed to date_diff() are instances of DateTimeInterface. The most common pattern involves using date_create_from_format() or simply passing the formatted string directly if you intend to let PHP parse it, but there is a nuance here.

date_diff() expects two objects. It does not automatically convert a string passed in the first argument into an object internally. Therefore, you must explicitly create an object for the current date/time as well. You can replace the date() call with new DateTime() or use DateTime::createFromFormat().

Here is the corrected implementation:

$date_expire = '2014-08-06 00:00:00';

// Incorrect: $date1 is a string
// $date1 = date("Y-m-d G:i:s"); 

// Correct: Create an object instance
$date1 = new DateTime(); // Represents current time, or use 'now'
// Alternatively, if you need a specific past/future time formatted as a string first:
// $date1_str = date("Y-m-d G:i:s"); 
// $date1 = DateTime::createFromFormat('Y-m-d G:i:s', $date1_str);

$date2 = new DateTime($date_expire); // Object created from string

$diff = date_diff($date1, $date2);
echo $diff->format('%d days');

In this corrected block, both $date1 and $date2 are objects. The date_diff() function can now successfully compute the difference between them, returning a DateInterval object that you can format as needed.

Why This Matters for Legacy Code Migration

This specific error frequently surfaces when developers migrate codebases from PHP 7 to PHP 8+. In PHP 7, while strict typing existed, certain internal handling of function arguments was slightly more lenient in edge cases involving date functions, or the codebase relied on date() returning a string that was implicitly cast elsewhere. However, date_diff() has always required objects; the error message simply became much clearer and more prominent in newer PHP versions due to improved type checking mechanisms.

If you encounter this while refactoring, do not assume the function accepts strings. Always inspect the first argument of date_diff(). If it is a string literal or the result of date(), instantiate it immediately. Using DateTime::createFromFormat() is often safer than relying on new DateTime('string') if your source data comes from user input or external APIs, as it allows you to validate the format string before attempting parsing.

For teams using the Laravel framework, this issue is less common because Laravel's facades like Carbon handle object creation automatically. If you are working with raw PHP or custom legacy modules within a modern stack, ensuring explicit object instantiation for date arithmetic is a necessary step to maintain compatibility with current engine standards.

Our network