Add 30 days to date

Add 30 days to date

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

Stefan Bogdanescu

Founder & Senior Architect

The Hidden Trap in PHP Date Formatting

The code snippet you are looking at contains a subtle but critical flaw that causes the calculation to fail entirely. When you pass a string like '05/06/2016' directly into strtotime(), PHP does not automatically assume the format you expect. Instead, it attempts to parse the date based on its internal rules, which often default to US order (Month/Day/Year) or fail to recognize the input if your local configuration expects a different standard.

In your specific example, date('05/06/2016', ...) implies a format of Month/Day/Year. However, without explicit formatting in the input string, strtotime() might interpret '05/06/2016' as May 6th, or worse, it might simply return the original timestamp if it cannot robustly parse the ambiguity, especially if the system locale prefers Day/Month/Year. But there is an even more fundamental issue at play: you are formatting the output string before passing it to strtotime().

The function signature for strtotime() accepts a timestamp (integer) or a readable date string. If you pass '05/06/2016' as a raw string without defining its format, strtotime() will attempt to parse it. If the parsing succeeds, it returns a Unix timestamp. The second argument of your date() function is that resulting timestamp.

Wait, looking closely at your code:

$next_due_date = date('05/06/2016', strtotime("+30 days"));

There are actually two distinct problems here. First, the format string '05/06/2016' inside the date() function is invalid syntax for a format. The date() function expects a format mask like 'Y-m-d', not a literal date value. You cannot use '05/06/2016' as a format string; it will treat every character literally, resulting in output that looks like the input or throws an error depending on the PHP version.

Second, and more likely the cause of your specific "no change" behavior, is how date() handles its arguments when you mix formats incorrectly. If you intended to convert a date string first, the order of operations is wrong. You need to parse the initial date into a timestamp, add 30 days to that timestamp, and then format it back into a readable string.

Here is the correct approach to achieve the desired result:

// Define the initial date in a standard, unambiguous format
$initial_date = '2016-05-06'; 

// Parse the string into a Unix timestamp
$timestamp = strtotime($initial_date);

// Add 30 days to the timestamp. 
// Note: strtotime('+30 days') expects a relative modifier to the current time 
// if used alone, but when passed as the first argument to date(), 
// it works on the timestamp provided in the second argument? 
// Actually, the standard usage is:
// $timestamp = strtotime($initial_date);
// $next_timestamp = strtotime('+30 days', $timestamp);

$next_due_date = date('Y-m-d', strtotime('+30 days', $timestamp));
echo $next_due_date; // Outputs: 2016-06-05

The key here is using strtotime() twice. The first call converts your readable string '2016-05-06' into an integer timestamp (seconds since the Unix epoch). The second call to strtotime('+30 days', $timestamp) tells PHP, "Take this specific timestamp and move 30 days forward." Finally, date('Y-m-d', ...) formats that new timestamp back into a readable string.

Using 'Y-m-d' for both input and output is a best practice in development and database interactions because it removes ambiguity regarding month/day ordering across different regions. If you are working within the Laravel ecosystem, this logic translates seamlessly to Carbon, which handles these date manipulations with less boilerplate code:

use Carbon\Carbon;

$next_due_date = Carbon::parse('2016-05-06')->addDays(30)->format('Y-m-d');
echo $next_due_date; 

This approach avoids the pitfalls of strtotime() parsing ambiguities and ensures that your date arithmetic is predictable regardless of server locale settings. Always verify that your input strings are in a format that can be reliably parsed, and always use a specific format mask for your output to ensure consistency in your application's logs or user-facing displays.

Our network