Adding Minutes to Date Time in PHP
In PHP, adding minutes to a datetime is a straightforward task. However, it may require a specific approach depending on the datetime format.
As mentioned in the inquiry, the given datetime format is "year-month-day hour:minute", e.g., "2011-11-17 05:05". To modify this datetime by adding minutes:
可以使用 DateTime 类和 DateInterval 类:
$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');
In this code:
The ISO 8601 standard is utilized for duration representation, where "P{y}Y{m1}M{d}DT{h}H{m2}M{s}S" denotes a duration of {y} years, {m1} months, {d} days, and so on.
For instance, "P1Y2DT5S" corresponds to 1 year, 2 days, and 5 seconds.
In the provided example, "PT" signifies a time interval, followed by "5M", which represents adding 5 minutes.
The above is the detailed content of How to Add Minutes to a DateTime String in PHP?. For more information, please follow other related articles on the PHP Chinese website!