到目前为止增加30分钟

Rus*_*wer 20 php timestamp date

所以我需要做的是增加30分钟到以下

date("Ymdhis");
Run Code Online (Sandbox Code Playgroud)

我试过这个

+strtotime("+30 minutes");
Run Code Online (Sandbox Code Playgroud)

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

Jos*_*osh 39

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

<?php

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

?>
Run Code Online (Sandbox Code Playgroud)

产量

2012/03/22 10:55:45
2012/03/22 11:25:45 // 30 minutes later
Run Code Online (Sandbox Code Playgroud)

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

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

http://codepad.org/Z5yquF55


Sup*_*oob 6

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

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

这是我的解决方案

 //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
Run Code Online (Sandbox Code Playgroud)