php 添加 30 分钟到日期

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

adding 30 minutes to date

phpdatetimestamp

提问by RussellHarrower

So what I need to do is add 30 minutes to the following

所以我需要做的是在下面添加 30 分钟

date("Ymdhis");

I tried this

我试过这个

+strtotime("+30 minutes");

however it does not seem to like it. I wondering what the correct why to do this is.

然而它似乎并不喜欢它。我想知道为什么这样做是正确的。

回答by Josh

Your method of using strtotimeshould work.

您的使用方法strtotime应该有效。

<?php

echo date("Y/m/d H:i:s", strtotime("now")) . "\n";
echo date("Y/m/d H:i:s", strtotime("+30 minutes"));

?>

Output

输出

2012/03/22 10:55:45
2012/03/22 11:25:45 // 30 minutes later

However your method of adding time probably isn't correct. The above will work to add 30 minutes to the current time. Suppose you want to add 30 minutes from a given time, $t, then use strtotime's second parameter, which is used as a base for the calculation of relative dates.

但是,您添加时间的方法可能不正确。以上将工作增加 30 分钟到当前时间。假设您要从给定时间添加 30 分钟$t,然后使用strtotime的第二个参数,该参数用作计算相对日期的基础。

date("Y/m/d H:i:s", strtotime("+30 minutes", $t));

http://codepad.org/Z5yquF55

http://codepad.org/Z5yquF55

回答by SuperNoob

I tested this code but it doesn't work for me:

我测试了这段代码,但它对我不起作用:

 $t = date();  
 date("Y/m/d h:i:s", strtotime("+30 minutes", $t));

Here's my solution

这是我的解决方案

 //This is where you put the date, but I use the current date for this example
 $date = date("Y-m-d H:i:s");

 //Convert the variable date using strtotime and 30 minutes then format it again on the desired date format
 $add_min = date("Y-m-d H:i:s", strtotime($date . "+30 minutes"));
 echo  $date . "<br />"; //current date or whatever date you want to put in here
 echo  $add_min; //add 30 minutes

回答by staticsan

strtotime()accepts a second parameter which is its starting point.

strtotime()接受作为起点的第二个参数。

If you have date("Ymdhis", $somedate)and wanted to add 30 minutes to it, you can do date("Ymdhis", strtotime("+30 minutes", $someddate))

如果你有date("Ymdhis", $somedate)并想增加 30 分钟,你可以这样做 date("Ymdhis", strtotime("+30 minutes", $someddate))

回答by Adam

Try something like.

尝试类似的东西。

$Start = "12:00:00";
$Minutes = 30;

$To = date("H:i:s", strtotime($Start)+($Minutes*60));

回答by sikander

Use this function:

使用这个功能:

date("Ymdhis", strtotime("+30 minutes"))

回答by Jacob S

<?php
print date("Y-m-d h:i:s", (time() + (60*30)) );
?>

回答by matt

Not sure what your entire code looks like, but:

不确定你的整个代码是什么样的,但是:

date("Ymdhis");

is returning a string. So it doesn't make sense to add the result of

正在返回一个字符串。所以添加结果没有意义

strtotime("+30 minutes");

(which is an integer) to that string.

(这是一个整数)到那个字符串。

You either want

你要么想要

strtotime("+30 minutes");

by itself, or

本身,或

date("Ymdhis", strtotime("+30 minutes"));

to get the formatted string.

获取格式化的字符串。

回答by mvdnes

Do you mean date("Ymdhis", strtotime("+30 minutes"));? This will represent the date that is 30 minutes in the future.

你的意思是date("Ymdhis", strtotime("+30 minutes"));?这将代表未来 30 分钟的日期。