Add 'x' number of hours to date

Add 'x' number of hours to date

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

Stefan Bogdanescu

Founder & Senior Architect

Adding Hours to Date in PHP: Beyond Simple String Arithmetic

In many scripting scenarios, you need to manipulate timestamps dynamically rather than hardcoding them. A common pattern involves taking the current system time and advancing it by a specific duration—say, 24 hours for a scheduled backup or 800 hours for a long-running maintenance window. The challenge lies in doing this without running into the pitfalls of naive string manipulation.

Your initial approach using date("Y-m-d H:m:s") produces a formatted string representing the current moment. However, adding an integer to that string directly (e.g., $now + $hours) will not yield the desired result. PHP does not perform arithmetic on date strings; it treats them as text unless they are explicitly converted into a numeric representation first. Attempting to concatenate or add numbers to this string format will simply append the number, resulting in invalid data like 2023-10-27 14:30:0050.

To correctly calculate a future timestamp, you must convert the current time into seconds since the Unix epoch and then perform the arithmetic there. The built-in time() function returns an integer representing the current Unix timestamp. This integer can be safely added to or subtracted from to shift the date forward or backward by any number of seconds.

Here is how you can implement this logic:

// Get current time in Unix timestamp format
$now = time(); 

// Define the number of hours to add (e.g., 24, 500, or 800)
$hours = 24; 

// Convert hours to seconds and add to the timestamp
$new_timestamp = $now + ($hours * 3600);

// Format the result back into a readable date string if needed
$new_time = date("Y-m-d H:i:s", $new_timestamp);

Once you have $new_time, it is formatted exactly like your original $now variable, making it easy to store in databases or log files. This method supports any range of hours, from a single minute (less than 24) up to large intervals like 800 hours, without losing precision.

Why Use Unix Timestamps?

Using the internal integer representation of time is more robust than working with DateTime objects for simple arithmetic tasks. While DateTime provides powerful features like timezone handling and relative duration calculations (e.g., adding "1 week"), converting to a timestamp for pure numerical addition keeps the logic straightforward and avoids potential overhead if you don't need those extra capabilities.

One important detail is consistency in formatting. Your original code uses H:m:s, which displays minutes with a leading zero (e.g., 09). The standard PHP constant for this is i (or i in the format string), not m. While date("Y-m-d H:m:s") works, using date("Y-m-d H:i:s") ensures you are following best practices and avoids confusion between minutes (i) and months (m).

If your application uses Laravel, this same logic applies seamlessly within Blade templates or controllers. You might see similar patterns in scheduled tasks or cron job configurations where precise timing is critical. For more complex scheduling needs involving human-readable intervals like "every 24 hours," you could explore Laravel's Carbon package or the built-in DateInterval class, but for straightforward hour additions, the timestamp method remains efficient and reliable.

Potential Pitfalls to Watch

The main risk with this approach is timezone mismatch. The time() function returns the local server time based on the system configuration. If your application expects UTC times while the server is configured to a different timezone, the calculated $new_time will be offset incorrectly relative to external systems expecting UTC. Always ensure that both the source timestamp and the target format align with your application's expected timezone context.

Our network