php - 将两个小时添加到日期变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7811609/
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 03:24:48  来源:igfitidea点击:

php - add two hours to date variable

phpdateaddstrtotime

提问by laarsk

I want to add 3 minutes to a date/time variable I have, but I'm not sure how to do this. I made the variable from a string like this: (which is in the RFC 2822 date format btw)

我想为我拥有的日期/时间变量添加 3 分钟,但我不确定如何执行此操作。我从这样的字符串中创建了变量:(顺便说一句,它采用 RFC 2822 日期格式)

$date = 2011-10-18T19:56:00+0200

I converted that string into date using this command:

我使用以下命令将该字符串转换为日期:

$time = date_format(DateTime::createFromFormat("Y-m-d\TH:i:sO", $date), "G:i")

Now, I'd like to add 3 minutes to that variable, but I I'm not sure how. I've used the following command in my script before, but that applies to the current date/time, so I'm not sure how to use that for my time variable:

现在,我想为该变量添加 3 分钟,但我不确定如何。我之前在脚本中使用过以下命令,但这适用于当前日期/时间,所以我不确定如何将它用于我的时间变量:

$currenttime = date('G:i', strtotime('+2 hours'));

So, how can I add three minutes to the $time variable?

那么,如何在 $time 变量中添加三分钟呢?

回答by saikat

echo $idate="2013-09-25 09:29:44";

$effectiveDate = strtotime("+40 minutes", strtotime($idate));

echo date("Y-m-d h:i:s",$effectiveDate);

回答by phihag

Use the second parameter of strtotimeto provide a reference time:

使用的第二个参数strtotime提供参考时间:

$date_rfc2822 = '2011-10-18T19:56:00+0200';
$dateTime = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date_rfc2822);
echo date('G:i', strtotime('+2 hours', $dateTime->getTimestamp()));

回答by Marc B

Since you're using the DateTime object already, stick with it:

由于您已经在使用 DateTime 对象,请坚持使用它:

$time = DateTime::createFromFormat("Y-m-d\TH:i:sO", $date);
$three_minutes = $time->add(new DateInterval('P2H'));
                                               ^^--two (2) hours (H)

回答by Logan

The format starts with the letter P, for "period." Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.

格式以字母 P 开头,表示“句点”。每个持续时间段由一个整数值表示,后跟一个时间段指示符。如果持续时间包含时间元素,则规范的该部分前面有字母 T。

http://www.php.net/manual/en/dateinterval.construct.php

http://www.php.net/manual/en/dateinterval.construct.php

That being said my solution to a similar problem is this:

话虽如此,我对类似问题的解决方案是这样的:

// pretend that $date is what you got from mysql
// which is like 2013-02-12 23:08:17
echo "<br>";
echo $date;
$time = DateTime::createFromFormat("Y-m-d H:i:s", $date);
echo "<br>";
echo $time->format('H-i-s');
$time->add(new DateInterval('PT2H'));
echo "<br>";
echo $time->format('H-i-s');
// Outputs:
// 2013-02-12 23:08:17
// 23-08-17
// 01-08-17

The thing is to add Pwhen using DateIntervalclass, and Tbefore time entries. For your case you need to go with PT3Mfor 3 minute addition. I was trying to add 2 hours and what I did was $time->add(new DateInterval('PT2H'));.

问题是P在使用DateInterval类时和T时间条目之前添加。对于您的情况,您需要PT3M添加 3 分钟。我试图增加 2 小时,而我所做的是$time->add(new DateInterval('PT2H'));.

If you would look at the interval specs:

如果您查看间隔规格:

Y   years
M   months
D   days
W   weeks. These get converted into days, so can not be combined with D.
H   hours
M   minutes
S   seconds

Mfor monthsand Mfor minutes. That is why there is a Tin front of time.

MmonthsMminutes。这就是为什么有一个T前面的时间。

At least that's what I want to believe... 'O_O

至少这就是我想相信的......'O_O

回答by Ahmed Al Bermawy

//set the date and time

$start = '2017-01-01 14:00:00';

//display the converted time you want to add for ex. 1 hour and 20 minutes 

echo date('Y-m-d H:i:s',strtotime('+1 hour +20 minutes',strtotime($start)));