$str = $_POST['text1'];まず、送信されたテキストをexplodeで/記号で分解し、年月日を配列で用意します。
$d = explode("/",$str);
$t1 = time();続いて、現在の日時と、用意した配列の値の日時のタイムスタンプをそれぞれ用意します。
$t2 = mktime(0,0,0,$d[1],$d[2],$d[0]);
$n = ceil(($t2 - $t1) / (60 * 60 * 24));2つのタイムスタンプの差を60×60×24で割れば、日数が出ます。ただし、今回は時分秒まで揃えていないので、端数が出るはずです。そこで、端数を切り上げるのに「ceil」という関数を使っています。これは小数点以下を切りあげた値を返す関数です。
※リストが表示されない場合
AddBlockなどの広告ブロックツールがONになっているとリストなどが表示されない場合があります。これらのツールをOFFにしてみてください。
<?php if ($_POST != null){ $str = $_POST['text1']; $d = explode("/",$str); $t1 = time(); $t2 = mktime(0,0,0,$d[1],$d[2],$d[0]); $n = ceil(($t2 - $t1) / (60 * 60 * 24)); $result = $str . "は、今日から {$n} 日後です。"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>sample page</title> </head> <body> <h1>Hello PHP!</h1> <p><?php echo $result; ?></p> <hr> <form method="post" action="./index.php"> <input type="text" name="text1"> <input type="submit"> </form> <hr> </body> </html>
<< 前へ |