在 PHP 中添加分钟到日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8169139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 04:05:59  来源:igfitidea点击:

Adding minutes to date time in PHP

phpdatetime

提问by Luke B

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.

我真的坚持将 X 分钟添加到日期时间,在进行了大量的 google'ing 和 PHP 手册阅读之后,我似乎没有任何进展。

The date time format I have is:

我的日期时间格式是:

2011-11-17 05:05: year-month-day hour:minute

2011-11-17 05:05year-month-day hour:minute

Minutes to add will just be a number between 0 and 59

要添加的分钟数只是 0 到 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?

有人可以给我一个有效的代码示例,因为我的尝试似乎并没有让我在任何地方?

回答by Tim Cooper

$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');

The ISO 8601 standard for durationis a string in the form of P{y}Y{m1}M{d}DT{h}H{m2}M{s}Swhere the {*}parts are replaced by a number value indicating how long the duration is.

持续时间ISO 8601 标准是一个字符串,P{y}Y{m1}M{d}DT{h}H{m2}M{s}S其中的{*}部分被一个数字值替换,该值指示持续时间的长度。

For example, P1Y2DT5Smeans 1 year, 2 days, and 5 seconds.

例如,P1Y2DT5S表示 1 年 2 天和 5 秒。

In the example above, we are providing PT5M(or 5 minutes) to the DateIntervalconstructor.

在上面的示例中,我们为构造函数提供PT5M(或 5 分钟)DateInterval

回答by Daniel

PHP's DateTime classhas a useful modifymethod which takes in easy-to-understand text.

PHP 的 DateTime 类有一个有用的修改方法,它接收易于理解的文本。

$dateTime = new DateTime('2011-11-17 05:05');
$dateTime->modify('+5 minutes');

You could also use string interpolation or concatenation to parameterize it:

您还可以使用字符串插值或串联来参数化它:

$dateTime = new DateTime('2011-11-17 05:05');
$minutesToAdd = 5;
$dateTime->modify("+{$minutesToAdd} minutes");

回答by Nemoden

$newtimestamp = strtotime('2011-11-17 05:05 + 16 minute');
echo date('Y-m-d H:i:s', $newtimestamp);

result is

结果是

2011-11-17 05:21:00

2011-11-17 05:21:00

Live demo is here

现场演示是 here

If you are no familiar with strtotimeyet, you better head to php.netto discover it's great power :-)

如果你还不熟悉strtotime,你最好php.net去发现它的强大:-)

回答by Brad

You can do this with native functions easily:

您可以使用本机函数轻松做到这一点:

strtotime('+59 minutes', strtotime('2011-11-17 05:05'));

I'd recommend the DateTime class method though, just posted by Tim.

不过,我建议使用 DateTime 类方法,它刚刚由 Tim 发布。

回答by acarito

I thought this would help some when dealing with time zones too. My modified solution is based off of @Tim Cooper's solution, the correct answer above.

我认为这在处理时区时也会有所帮助。我修改后的解决方案基于@Tim Cooper 的解决方案,即上面的正确答案。

$minutes_to_add = 10;
$time = new DateTime();
**$time->setTimezone(new DateTimeZone('America/Toronto'));**
$time->add(new DateInterval('PT' . $minutes_to_add . 'M'));
$timestamp = $time->format("Y/m/d G:i:s");

The bold line, line 3, is the addition. I hope this helps some folks as well.

粗线,第 3 行,是加法。我希望这也能帮助一些人。

回答by Peter

A bit of a late answer, but the method I would use is:

有点晚的答案,但我会使用的方法是:

// Create a new \DateTime instance
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-26 10:00:00');

// Modify the date
$date->modify('+5 minutes');

// Output
echo $date->format('Y-m-d H:i:s');

Or in PHP >= 5.4

或者在 PHP >= 5.4

echo (DateTime::createFromFormat('Y-m-d H:i:s', '2015-10-26 10:00:00'))->modify('+5 minutes')->format('Y-m-d H:i:s')

回答by Beaudinn Greve

If you want to give a variable that contains the minutes.

如果你想给出一个包含分钟的变量。

Then I think this is a great way to achieve this.

然后我认为这是实现这一目标的好方法。

$minutes = 10;
$maxAge = new DateTime('2011-11-17 05:05');
$maxAge->modify("+{$minutes} minutes");

回答by DeathRs

Use strtotime("+5 minute", $date);

strtotime("+5 minute", $date);



Example:例子:

$date = "2017-06-16 08:40:00";
$date = strtotime($date);
$date = strtotime("+5 minute", $date);
echo date('Y-m-d H:i:s', $date);

回答by user3361395

I don't know why the approach set as solution didn't work for me. So I'm posting here what worked for me in hope it can help anybody:

我不知道为什么设置为解决方案的方法对我不起作用。所以我在这里发布对我有用的东西,希望它可以帮助任何人:

$startTime = date("Y-m-d H:i:s");

//display the starting time
echo '> '.$startTime . "<br>";

//adding 2 minutes
$convertedTime = date('Y-m-d H:i:s', strtotime('+2 minutes', strtotime($startTime)));

//display the converted time
echo '> '.$convertedTime;

回答by Robert Schwindaman

As noted by Brad and Nemoden in their answers above, strtotime() is a great function. Personally, I found the standard DateTime Object to be overly complicated for many use cases. I just wanted to add 5 minutes to the current time, for example.

正如 Brad 和 Nemoden 在上面的回答中指出的那样, strtotime() 是一个很棒的函数。就个人而言,我发现标准 DateTime 对象对于许多用例来说过于复杂。例如,我只想为当前时间增加 5 分钟。

I wrote a function that returns a date as a string with some optional parameters:
1.) time:String | ex: "+5 minutes" (default = current time)
2.) format:String | ex: "Y-m-d H:i:s" (default = "Y-m-d H:i:s O")

我编写了一个函数,该函数将日期作为带有一些可选参数的字符串返回:
1.) time:String | 例如:“+5 分钟”(默认值 = 当前时间)
2.) 格式:字符串 | 例如:“Ymd H:i:s”(默认值 =“Ymd H:i:s O”)

Obviously, this is not a fully featured method. Just a quick and simple function for modifying/formatting the current date.

显然,这不是一个功能齐全的方法。只是一个用于修改/格式化当前日期的快速简单的功能。

function get_date($time=null, $format='Y-m-d H:i:s O')
{
    if(empty($time))return date($format);
    return date($format, strtotime($time));
}

// Example #1: Return current date in default format
$date = get_date(); 

// Example #2: Add 5 minutes to the current date
$date = get_date("+5 minutes"); 

// Example #3: Subtract 30 days from the current date & format as 'Y-m-d H:i:s'
$date = get_date("-30 days", "Y-m-d H:i:s");