php - 添加3分钟到日期变量 [英] php - add 3 minutes to date variable

查看:360
本文介绍了php - 添加3分钟到日期变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个日期/时间变量3分钟,但我不知道如何做到这一点。我从一个这样的字符串中创建了变量:(它是RFC 2822日期格式btw)

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

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

  $ time = date_format(DateTime :: createFromFormat(Ymd\TH:i:sO,$ date),G: i)

现在,我想添加3分钟到该变量,但我'不知道怎么样我以前在我的脚本中使用了以下命令,但是这适用于当前的日期/时间,所以我不知道如何使用我的时间变量:

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

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



我以前尝试过:

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

但是当前添加了3分钟的时间,它没有使用$ date变量...



我试过:

  $ time = DateTime :: createFromFormat (Ymd\TH:i:sO,$ date); 
$ time = $ time-> add(new DateInterval('P2H'));

但是当我做

  echo date_format($ time,'G:i'); 

没有回应...



任何帮助?

解决方案

您可以使用 strtotime 两次: / p>

  $ date = strtotime('2011-10-18T19:56:00 + 0200'); 
echo date('G:i',strtotime('+ 3 minutes',$ date));


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?

I tried this before:

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

but that gives the current time with 3 minutes added, it doesnt use the $date variable...

And I tried:

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

But then when I do

echo date_format($time, 'G:i');

nothing is echoed...

Any help here?

解决方案

You could just use strtotime twice:

$date = strtotime('2011-10-18T19:56:00+0200');
echo date('G:i', strtotime('+3 minutes', $date));

这篇关于php - 添加3分钟到日期变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆