Adding Minutes to a Date Time in PHP: A Comprehensive Solution
Question: How can I increment a date time object in PHP by a specified number of minutes while preserving the original format?
Answer:
To effectively add minutes to a date time in PHP, we will employ a combination of the DateTime and DateInterval classes. Here's a detailed breakdown:
$minutes_to_add = 5; $time = new DateTime('2011-11-17 05:05'); $time->add(new DateInterval('PT' . $minutes_to_add . 'M')); $stamp = $time->format('Y-m-d H:i');
Explanation:
The ISO 8601 standard for duration follows a specific string format. Here, 'P{y}Y{m1}M{d}DT{h}H{m2}M{s}S' represents a duration with various components, such as years ('Y'), months ('M'), days ('D'), hours ('H'), minutes ('M'), and seconds ('S'). In our case, we utilize 'PT5M' to indicate adding 5 minutes to the date time.
The above is the detailed content of How to Add Minutes to a Date Time Object in PHP?. For more information, please follow other related articles on the PHP Chinese website!