Adding days to a timestamp

Adding days to a timestamp

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

Stefan Bogdanescu

Founder & Senior Architect

Adding days to a timestamp: The hidden trap in PHP date arithmetic

Calculating future dates is one of those tasks that seems trivial until it breaks in production. You start with a known date, add a fixed number of seconds representing a week, and expect the calendar to roll forward by seven days. In most cases, this logic holds true. However, when you cross specific boundaries—like the transition from October 31st to November 1st in 2010—you might find your dates jumping unexpectedly.

Consider a scenario where you calculate mktime for October 28th and then add four weeks of seconds (60*60*24*7). If you start on October 1st, the output is consistent: every step lands exactly seven days later in the same month. But if you start on October 28th, the second calculated date jumps from November 4th to November 3rd.

This discrepancy isn't a bug in your logic; it's a symptom of how mktime handles timezones and Daylight Saving Time (DST). When you add seconds to a timestamp without accounting for DST transitions, PHP assumes the new moment is always UTC-equivalent or local standard time, ignoring that clocks might shift backward. In this specific case, the transition from October 31st to November 1st in many timezones involved a clock change where the day effectively "gained" an hour (or lost one, depending on the direction of the shift).

When you add exactly 7 days (604,800 seconds) to a timestamp that sits right before a DST transition, PHP adds the seconds blindly. If the clocks fall back by one hour at midnight, adding 24 hours worth of seconds doesn't move you 24 hours forward in wall-clock time; it moves you 25 hours forward relative to the shifted local time. This causes the date calculation to drift off by an entire day if you aren't careful about how you interpret the resulting timestamp.

The solution is to avoid relying solely on raw second arithmetic when crossing month or DST boundaries. Instead, use functions designed for date manipulation that abstract away these complexities. The DateTime class in PHP was built specifically for this purpose. It handles timezone transitions and calendar logic internally, ensuring that "adding one week" always results in a human-readable date shift of exactly seven days, regardless of local clock adjustments.

Here is how you can refactor the original approach using DateTime:

$date = new DateTime('2010-10-28');
$interval = DateInterval::createFromDateString('7 days');

for ($i = 0; $i < 5; $i++) {
    echo "Date=" . $date->format('d-m-Y') . "\n";
    $date->add($interval);
}

This approach guarantees that every iteration advances the date by exactly one week in calendar terms. The DateTime object manages the underlying timestamp adjustments required to handle DST transitions correctly, preventing the kind of off-by-one errors seen when manually adding seconds with mktime.

If you are working within a Laravel application, the built-in helpers often wrap these functionalities, but understanding the underlying cause is crucial for debugging edge cases. For more robust date handling in complex applications, consider exploring dedicated libraries or the official Laravel documentation on date helpers. While LaravelCompany offers insights into broader ecosystem tools, the core fix here relies on switching from arithmetic manipulation to semantic date intervals.

The key takeaway is that raw timestamp arithmetic is fragile near DST boundaries and month ends. By leveraging DateTime and DateInterval, you delegate the complex logic of timezones and calendar rules to a robust implementation, ensuring your dates remain accurate no matter when or where they are calculated.

Our network