how do I subtract 24 hour from date time object in PHP

how do I subtract 24 hour from date time object in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

The String vs. Timestamp Trap in PHP DateTime Math

The error A non well formed numeric value encountered usually points to a specific type mismatch in PHP's internal logic: you are trying to perform arithmetic on a string instead of an integer. In the snippet you provided, the root cause lies in the first line of your code.

$now = date("Y-m-d H:m:s");
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));

Here, $now is assigned the output of date(), which returns a formatted string like "2023-10-27 14:30:00". When you pass this string as the second argument to strtotime(), PHP attempts to parse it. While strtotime() is robust enough to handle ISO-8601 strings, passing a raw formatted string as the "base time" is fragile. More critically, if there are any subtle encoding issues or if the environment expects an integer epoch for that specific operation, the math fails before it even completes.

However, the real issue often surfaces when you try to use $now in subsequent calculations later in your script. Once a variable holds a formatted string, it ceases to be a numeric timestamp. You cannot subtract 24 hours from a string representation of time using standard arithmetic operators (-). The solution is to ensure that the value representing "now" is always an integer timestamp (Unix epoch) when performing math.

To fix this immediately, change your initial variable assignment:

// Store as an integer timestamp first
$now = time(); 
$date = date("Y-m-d H:m:s", strtotime('-24 hours', $now));

By using time(), you guarantee that $now is a valid integer representing seconds since the Unix epoch. When passed to strtotime(), it works reliably, and the resulting formatted string can be safely stored in a different variable if needed for display later. This separation of concerns—keeping calculation variables as integers and output variables as strings—is a fundamental best practice in PHP to avoid exactly the "non well formed numeric" error you encountered.

Using DateTime Objects for Precision

While fixing the immediate bug involves switching to time(), relying on string formatting for logic is generally discouraged in modern PHP development. The DateTime class offers a cleaner, more object-oriented approach that handles time zones and leap seconds automatically.

If you are working on a larger application, consider this pattern:

$now = new DateTime();
$date = clone $now;
$date->modify('-24 hours');

echo $date->format('Y-m-d H:i:s');

This approach decouples the data type from the operation. The object holds the state, and you modify that state using modify(). This is safer because it prevents accidental string-to-number coercion errors when passing values between functions. It also makes your code self-documenting; reading ->modify('-24 hours') immediately tells a developer exactly what happened to the time, whereas manipulating raw timestamps can be opaque without comments.

Trade-offs and Version Considerations

There are slight differences in behavior depending on the PHP version, particularly regarding timezone handling. If you run this code in an environment with date_default_timezone_set() configured (or not), strtotime interprets relative times based on that context.

If you are maintaining legacy codebases, you might encounter scenarios where strtotime throws warnings if the base timestamp is a string containing special characters or unexpected whitespace. The integer timestamp approach bypasses these parsing quirks entirely. However, integers can exceed the 32-bit limit in older PHP versions (pre-7.0) if dealing with dates far in the future or past, though this rarely affects current "now" calculations.

For enterprise applications, specifically those built on the Laravel framework, the Carbon library provides an even more robust interface for these datetime manipulations, often wrapping the native DateTime classes. If you are building a new project and want to avoid these low-level timestamp pitfalls entirely, integrating a helper library like Carbon via Composer is a standard practice that abstracts away the string-vs-integer confusion completely.

Ultimately, the error you saw was a classic case of mixing data types. By ensuring your time variables remain integers during calculation phases and only convert them to strings for output, you eliminate the ambiguity that causes PHP to throw numeric value errors.

Our network