PHP date format, remove time and more

PHP date format, remove time and more

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

Stefan Bogdanescu

Founder & Senior Architect

Manipulating PHP Date Formats and Calculating Elapsed Time

When working with dates in PHP, the temptation is often to rely on string manipulation functions like substr or str_replace. While this works for simple formatting tasks where you know exactly how the data is structured, it breaks down the moment timezones shift or input formats vary. The robust approach involves using the DateTime and DateInterval classes. These built-in tools handle the complexity of calendar logic, leap years, and daylight saving time adjustments automatically.

To strip the time component from a datetime string like '2012-09-09 03:09:00', you have two primary options depending on your needs. The most straightforward method is to format the DateTime object directly. By creating an instance of DateTime with your input string and then calling the format() method, you can output a specific pattern. Using the 'Y-m-d' pattern ensures the time portion is ignored entirely in the output.

$date = new DateTime('2012-09-09 03:09:00');
$cleanDate = $date->format('Y-m-d'); 
// Result: "2012-09-09"

Alternatively, you can use the setTime() method to explicitly set the time components of the object to zero (midnight) before formatting. This is functionally equivalent for string representation but conceptually distinct; it modifies the internal state of the object to represent a date without a specific time.

$date = new DateTime('2012-09-09 03:09:00');
$date->setTime(0, 0, 0);
$cleanDate = $date->format('Y-m-d'); 
// Result: "2012-09-09"

The second requirement is calculating the elapsed time since that specific date up to the server's current moment. This requires creating a second DateTime instance for the current time and comparing it against the original. The standard tool for this comparison is the diff() method, which returns a DateInterval object. This object contains properties like y, m, d, h, i, and s representing years, months, days, hours, minutes, and seconds respectively.

$start = new DateTime('2012-09-09 03:09:00');
$end = new DateTime(); // Uses server timezone by default

$interval = $start->diff($end);

// Accessing the properties of the interval
echo "Years passed: " . $interval->y;
echo "Days passed: " . $interval->days;
echo "Hours passed: " . $interval->h;

It is crucial to understand the distinction between $interval->d and $interval->days. The days property represents the total number of days in the interval, whereas d represents the remaining days after extracting full years and months. If you are only interested in the raw count of calendar days passed regardless of month boundaries, $interval->days is usually the safer choice for high-level reporting.

A common pitfall involves timezones. The new DateTime() constructor defaults to the server's timezone setting found in date_default_timezone_set(). If your application runs on a server configured to UTC but your database stores times in Eastern Time, your calculations will be off by several hours. Always ensure that both the input date and the current time reference are in the same timezone before performing arithmetic or comparisons.

For developers working within the Laravel ecosystem, these native PHP classes integrate seamlessly. However, if you prefer a more expressive syntax for building dates from parts (year, month, day) rather than parsing strings, the Carbon package is often the standard choice. You can install it via Composer and use methods like now()->diffInYears($date) which return integers directly without needing to inspect interval properties manually.

composer require carbonphp/carbon-doctrine-types

While Laravel provides helpers, sticking to the core DateTime classes ensures your logic remains decoupled from framework specifics unless you have a specific reason to upgrade to Carbon's extensive feature set.

Our network