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

发布于 2024-12-11 01:49:27 字数 470 浏览 1 评论 0原文

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

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

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

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

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

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

那么,如何向 $time 变量添加三分钟?

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)

$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:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

澜川若宁 2024-12-18 01:49:28
echo $idate="2013-09-25 09:29:44";

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

echo date("Y-m-d h:i:s",$effectiveDate);
echo $idate="2013-09-25 09:29:44";

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

echo date("Y-m-d h:i:s",$effectiveDate);
浅忆流年 2024-12-18 01:49:28

由于您已经在使用 DateTime 对象,因此请继续使用它:

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

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

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

使用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()));

Use the second parameter of strtotime to provide a reference time:

$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()));
画尸师 2024-12-18 01:49:28

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

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

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

// 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

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

如果您查看间隔规格:

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

M 表示M 表示分钟。这就是为什么时间前面有一个T

至少我愿意相信这一点......'O_O

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.

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 P when using DateInterval class, and T before time entries. For your case you need to go with PT3M for 3 minute addition. I was trying to add 2 hours and what I did was $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

M for months and M for minutes. That is why there is a T in front of time.

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

旧梦荧光笔 2024-12-18 01:49:28
//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)));
//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)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文