web-dev-qa-db-ja.com

現在まで30分追加

だから、私がする必要があるのは、以下に30分を追加することです

date("Ymdhis");

私はこれを試しました

+strtotime("+30 minutes");

しかし、それは好きではないようです。なぜこれを行うのが正しいのか疑問に思います。

18
RussellHarrower

strtotimeを使用する方法が機能するはずです。

<?php

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

?>

出力

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

ただし、時間を追加する方法はおそらく正しくありません。上記は、現在の時間に30分を追加するために機能します。特定の時間から30分を追加するとします。$t、次にstrtotimeの2番目のパラメーターを使用します。これは、相対日付の計算のベースとして使用されます。

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

http://codepad.org/Z5yquF55

37
Josh

このコードをテストしましたが、うまくいきません。

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

これが私の解決策です

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

のようなものを試してください。

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

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

strtotime()は、開始点である2番目のパラメーターを受け入れます。

date("Ymdhis", $somedate)があり、30分追加したい場合は、date("Ymdhis", strtotime("+30 minutes", $someddate))を実行できます。

2
staticsan

この機能を使用します。

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

コード全体がどのように見えるかはわかりませんが、次のとおりです。

date("Ymdhis");

文字列を返しています。したがって、次の結果を追加しても意味がありません

strtotime("+30 minutes");

(整数)その文字列に。

どちらかが欲しい

strtotime("+30 minutes");

単独で、または

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

フォーマットされた文字列を取得します。

0
matt

date("Ymdhis", strtotime("+30 minutes"));のことですか?これは、将来の30分である日付を表します。

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