Adding minutes to date time in PHP

Adding minutes to date time in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Adding Minutes to Date Time in PHP

Handling time arithmetic in PHP requires understanding how the language represents temporal data internally. If you are working with a simple string like 2011-11-17 05:05 and need to add a specific number of minutes, attempting to parse that string into a new variable or using basic string manipulation will quickly lead to errors. The most robust approach involves converting your string representation into a standardized datetime object, performing the arithmetic there, and then formatting the result back to your desired output format.

The standard library function DateTime::modify() is the primary tool for this task. It accepts a relative time string, such as "+30 minutes", and updates the internal timestamp accordingly. To use this, you must first instantiate a DateTime object from your input string. This step converts the human-readable format into an object that PHP understands how to manipulate mathematically.

$input = '2011-11-17 05:05';
$minutesToAdd = 45;

// Create a DateTime instance from the string
$date = new DateTime($input);

// Use modify to handle the arithmetic
$date->modify("+{$minutesToAdd} minutes");

// Format back to the original style (Y-m-d H:i)
$output = $date->format('Y-m-d H:i');

echo $output; // Outputs: 2011-11-17 06:50

In this example, the DateTime constructor parses the year-month-day hour:minute string. Once instantiated, the modify() method interprets the string +45 minutes. It calculates the new timestamp and updates the object's properties. Finally, the format() method ensures the output matches your specific input requirement exactly. If you omit the time portion in your format string, PHP defaults to the full representation provided by the constructor, which preserves the original structure unless explicitly changed.

A critical pitfall in date arithmetic is handling day rollovers and leap years. Adding minutes does not typically trigger a month or year change, but if your calculation logic expands to include hours or days, you must ensure your format() string handles these transitions gracefully. For instance, adding 45 minutes to 05:15 results in 06:00. If you were adding 45 minutes to a time that crosses into the next hour without padding the seconds, relying solely on the input format might seem sufficient, but it is best practice to always specify the full format mask (Y-m-d H:i:s) during formatting unless seconds are strictly absent from your data model.

If you are working within the Laravel ecosystem, you can leverage the Carbon package, which extends PHP's native date capabilities with a fluent interface. While DateTime is perfectly adequate for simple additions, Carbon offers more readable syntax and handles edge cases like timezones automatically if configured correctly.

use Illuminate\Support\Carbon;

$input = '2011-11-17 05:05';
$minutesToAdd = 45;

$date = Carbon::parse($input);
$date->addMinutes($minutesToAdd);

$output = $date->format('Y-m-d H:i');

Using Carbon::parse() achieves the same result as the native DateTime constructor but integrates seamlessly with Laravel's date helpers. The addMinutes() method returns a new instance, allowing for method chaining which improves code readability. This approach is particularly useful if your application relies on Carbon for other datetime operations later in the request lifecycle.

Ultimately, the choice between native DateTime and Carbon depends on your project requirements. For standalone scripts or simple additions, native functions keep dependencies low. For Laravel applications, Carbon is often the standard, offering a consistent API across the framework's various components. Regardless of the method chosen, relying on object-oriented date manipulation rather than string math ensures accuracy and prevents common off-by-one errors inherent to manual calculation.

Our network