在 PHP 中添加分钟到日期时间 [英] Adding minutes to date time in PHP
问题描述
在进行了大量的 google'ing 和 PHP 手册阅读之后,我真的坚持在日期时间中添加 X 分钟,但我似乎一无所获.
I'm really stuck with adding X minutes to a datetime, after doing lots of google'ing and PHP manual reading, I don't seem to be getting anywhere.
我的日期时间格式是:
2011-11-17 05:05
:年-月-日小时:分钟
要添加的分钟数只是 0 到 59 之间的数字
Minutes to add will just be a number between 0 and 59
我希望输出与添加分钟的输入格式相同.
I would like the output to be the same as the input format with the minutes added.
有人能给我一个有效的代码示例吗,因为我的尝试似乎没有任何效果?
Could someone give me a working code example, as my attempts don't seem to be getting me anywhere?
推荐答案
$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');
ISO 8601 持续时间标准 是 形式的字符串P{y}Y{m1}M{d}DT{h}H{m2}M{s}S
其中 {*}
部分被替换为指示如何持续时间很长.
The ISO 8601 standard for duration is a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}S
where the {*}
parts are replaced by a number value indicating how long the duration is.
例如P1Y2DT5S
表示1年2天5秒.
For example, P1Y2DT5S
means 1 year, 2 days, and 5 seconds.
在上面的示例中,我们为 DateInterval
构造函数提供了 PT5M
(或 5 分钟).
In the example above, we are providing PT5M
(or 5 minutes) to the DateInterval
constructor.
这篇关于在 PHP 中添加分钟到日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!