How do I add 24 hours to a unix timestamp in php?

How do I add 24 hours to a unix timestamp in php?

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

Stefan Bogdanescu

Founder & Senior Architect

The Wrong Way and the Right Way to Add Time in PHP

The most immediate question developers ask when dealing with time is, "What is the number for 24 hours so I can just add it?" The instinctive answer is often a hard-coded constant like 86400. While this works arithmetically, relying on raw seconds for duration calculations is a fragile pattern. It couples your logic to specific units and forces you to remember that one hour equals 3600 seconds or that a week requires multiplying by seven first. If you ever need to handle milliseconds or leap seconds in the future, your code breaks because it lacks semantic clarity.

A superior approach leverages PHP's built-in date manipulation functions. The DateTime class and its companion DateInterval are designed specifically for this purpose. They allow you to express time durations in terms of years, months, weeks, days, hours, minutes, or seconds, rather than forcing every duration into a single integer count of seconds.

To add 24 hours to the current Unix timestamp, you do not need to calculate the value of an hour manually. Instead, you construct an interval representing one day and apply it to a date object. This method is robust because it handles edge cases internally, though in the context of pure Unix timestamps (which ignore calendar months), the arithmetic remains linear.

Here is a practical implementation using DateTime:

$now = new DateTime();
$interval = new DateInterval('P1DT0H0M0S'); // P1D represents one day

$futureTime = $now->modify($interval);

// Get the resulting Unix timestamp
$timestampWith24Hours = $futureTime->getTimestamp();

The DateInterval string 'P1DT0H0M0S' follows the ISO 8601 standard. The P marks the start of the period, followed by Y, M, D, T, H, and S. You can simply change 1 to 2 to add 48 hours, or 7 to add a week. This approach keeps your code self-documenting; reading "add one day" is far more intuitive than seeing + 86400.

If you prefer working strictly with integers and the strtotime function, PHP allows you to pass relative strings directly to the timestamp generator. You can take the current time, add a string description of the duration, and convert it back to a Unix integer in one step:

$timestamp = strtotime('+1 day'); 
// Or for 48 hours
$timestamp = strtotime('+2 days');
// Or more explicitly with hours
$timestamp = strtotime('+24 hours');

This method is concise and leverages the parser's ability to understand English-like time phrases. However, be cautious when mixing this with specific date formats or timezone calculations, as strtotime can sometimes behave unexpectedly with ambiguous inputs compared to the explicit logic of DateTime.

When adding durations to Unix timestamps, remember that these values are always in UTC seconds since January 1, 1970. If your application relies on local timezones for display but stores data in UTC, you must ensure you convert back to the user's timezone only when presenting the data. Adding an interval to a DateTime object preserves timezone information automatically if the object is initialized with one, whereas raw timestamp arithmetic strips that context until you explicitly re-apply it.

For high-performance applications where object instantiation overhead matters, or for simple scripts, calculating the seconds directly via 86400 * 1 is valid. However, this should be reserved for cases where performance profiling confirms that DateTime operations are a bottleneck. The semantic cost of writing magic numbers usually outweighs the negligible CPU difference in modern environments.

If you are building a Laravel application, consider using the Carbon library, which extends these native PHP classes with a more fluent interface. While not strictly necessary for simple arithmetic, it reduces boilerplate code significantly:

use Carbon\Carbon;

$future = Carbon::now()->addDay(); // Adds 24 hours
$timestamp = $future->timestamp;

This abstraction allows you to chain operations like addHours(24), making the intent of your code impossible to misinterpret. Whether you choose raw arithmetic, native PHP classes, or a third-party library, the principle remains the same: prefer semantic duration strings over hardcoded second counts whenever possible to ensure maintainability.

Our network