Adding Minutes to Date and Time in PHP
Adding minutes to a datetime is a common task that can be achieved using PHP's built-in DateTime interface. As you mentioned, you've encountered difficulties despite consulting online resources. Let's provide a detailed solution to your problem.
Your input datetime format is a combination of year, month, day, hour, and minute, using the ISO 8601 standard. You aim to add an arbitrary number of minutes to this datetime and return the result in the same format.
Here's a PHP code example that will fulfill your requirement:
<?php $minutes_to_add = 5; // Let's add 5 minutes // Initialize a new DateTime object using the input format $time = new DateTime('2011-11-17 05:05'); // Create a DateInterval object to represent the minutes to add $interval = new DateInterval('PT' . $minutes_to_add . 'M'); // Add the interval to the datetime using the add() method $time->add($interval); // Format the resulting datetime in the original format $stamp = $time->format('Y-m-d H:i'); echo $stamp; // Output: 2011-11-17 05:10 ?>
Here's how the code works:
The above is the detailed content of How to Add Minutes to a Date and Time in PHP?. For more information, please follow other related articles on the PHP Chinese website!