下記の事をやりたいのですが教えてください。
日付を選択できるセレクトボックスを設置しました。
そのセレクトボックは、当日の日付が選択されるようになっています(今日が27日であれば、27日と。)
これを今日の日付からプラス2日後の日付にするにはどうのようにしたらよいでしょか?
一番詳細な回答をいただけた方に500ポイント差し上げます
サンプルソースをいただけると助かります
よろしくお願いします。
ご質問にある「日付を選択できるセレクトボックスを設置しました」が分からないので、一通り動くプログラムを書きに掲げます。
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form> <select> <?php //閏年判定 function isleap($year) { $ret = FALSE; if ($year % 4 == 0) $ret = TRUE; if ($year % 100 == 0) $ret = FALSE; if ($year % 400 == 0) $ret = TRUE; return $ret; } //月の日数 function getDaysInMonth($year, $month) { static $days = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if ($month < 1 || $month > 12) return FALSE; $days[2] = isleap($year) ? 29 : 28; //閏年の判定 return $days[$month]; } $ti = strtotime('+2 day'); //本日から2日後 $year = date('Y', $ti); $month = date('n', $ti); $day = date('j', $ti); $day_max = getDaysInMonth($year, $month); for ($d = 1; $d <= $day_max; $d++) { $selected = ($d == $day) ? 'selected' : ''; echo "<option value=\"{$d}\" {$selected}>{$d}</option>\n"; } ?> </select> </form> </body> </html>
肝になるのは、strtotime関数です。
$two_days = mktime(0, 0, 0, date("m") , date("d")+2, date("Y"));
echo date(Ymd,$two_days);
?>
二日後の日付を表示するプログラムです。
二日後のタイムスタンプを生成し($two_days)、
それを年月日の形に生成して出力しています
フォームにデータを流し込む部分は、
使用しているフレームワーク等によるので割愛します
URLを参考に、適切な形に成型して出力してください
ご質問にある「日付を選択できるセレクトボックスを設置しました」が分からないので、一通り動くプログラムを書きに掲げます。
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form> <select> <?php //閏年判定 function isleap($year) { $ret = FALSE; if ($year % 4 == 0) $ret = TRUE; if ($year % 100 == 0) $ret = FALSE; if ($year % 400 == 0) $ret = TRUE; return $ret; } //月の日数 function getDaysInMonth($year, $month) { static $days = array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); if ($month < 1 || $month > 12) return FALSE; $days[2] = isleap($year) ? 29 : 28; //閏年の判定 return $days[$month]; } $ti = strtotime('+2 day'); //本日から2日後 $year = date('Y', $ti); $month = date('n', $ti); $day = date('j', $ti); $day_max = getDaysInMonth($year, $month); for ($d = 1; $d <= $day_max; $d++) { $selected = ($d == $day) ? 'selected' : ''; echo "<option value=\"{$d}\" {$selected}>{$d}</option>\n"; } ?> </select> </form> </body> </html>
肝になるのは、strtotime関数です。
解答ありがとうございます。ちょっと
後出しで申し訳ないのですが、下記のコードで回収した場合、どのようにしたらよいでしょうか??
<select class="age" name="month01">
$max_month = 12;
$max_day = 31;
$this_month = date(m);
$this_day = date(d);
$mikomi_month = $this_month;
$mikomi_day = $this_day;
if($mikomi_day>$max_day) {
$mikomi_day -= $max_day;
$mikomi_month = $this_month+1;
if($this_month>$max_month) {
$mikomi_month = $this_month-$max_month;
}
}
for($i=1;$i<=$max_month;$i++) {
?>
<option value="<?php echo $i;?>月" <?php if($i==$mikomi_month){echo "selected=\"selected\"";}?> >
</option>
}
?>
</select>
月
<select class="age" name="day01">
for($i=1;$i<=$max_day;$i++) {
?>
<option value="<?php echo $i;?>日" <?php if($i==$mikomi_day){echo "selected=\"selected\"";}?> >
</option>
}
?>
</select>
日
<select class="age" name="oclock01" onblur="Validator.check(this)">
<option value="10:00~12:00" selected="selected">10:00~12:00</option>
<option value="12:00~13:30" >12:00~13:30</option>
<option value="15:00~17:00">15:00~17:00</option>
<option value="17:00~19:30">17:00~19:30</option>
<option value="何時でもOK">何時でもOK</option>
</select>
コメントに頂いたソースは、年の繰り上げが間違っているようなので、下記のように改めてみました。ご確認下さい。
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form> <?php $month_max = 12; $day_max = 31; $ti = strtotime('+2 day'); //本日から2日後 $year = date('Y', $ti); $month = date('n', $ti); $day = date('j', $ti); //月 echo "<select class=\"age\" name=\"month01\">\n"; for ($m = 1; $m <= $month_max; $m++) { $selected = ($m == $month) ? 'selected="selected"' : ''; echo "<option value=\"{$m}\" {$selected}>{$m}</option>\n"; } echo "</select>月\n"; //日 echo "<select class=\"age\" name=\"day01\">\n"; for ($d = 1; $d <= $day_max; $d++) { $selected = ($d == $day) ? 'selected="selected"' : ''; echo "<option value=\"{$d}\" {$selected}>{$d}</option>\n"; } echo "</select>日\n"; ?> <select class="age" name="oclock01" onblur="Validator.check(this)"> <option value="10:00~12:00" selected="selected">10:00~12:00</option> <option value="12:00~13:30" >12:00~13:30</option> <option value="15:00~17:00">15:00~17:00</option> <option value="17:00~19:30">17:00~19:30</option> <option value="何時でもOK">何時でもOK</option> </select> </form> </body> </html>
ありがとうございます。たいへん助かりました。
ですが、お恥ずかしながら、ちょっとうまくいかない箇所がありまして・・・
http://www.shinjukubc-t.com/01counseling/index.php
上記にTESTUPしましたが、"ご予約希望日"の"第1希望"の月の前に余計なセレクトボックスが出てしまいました、こちらの消し方を教えてください。
また、良かったらなのですが、こーいうような相談を、何故か受ける機会が多いので、
ちゃんとした費用での案件紹介は興味ありますでしょうか?
もし、ありましたら、ご連絡先をお伝えしますので、よろしくお願いいたします。
ですが、お恥ずかしながら、ちょっとうまくいかない箇所がありまして・・・
以下の行が重複しています。
<select class="age" name="month01">
HTMLとPHPとで二重に同じselect文を書いていませんか? ご確認下さい。
また、良かったらなのですが、こーいうような相談を、何故か受ける機会が多いので、
ちゃんとした費用での案件紹介は興味ありますでしょうか?
せっかくのお誘いですが、興味はありません。
解答ありがとうございます。ちょっと
後出しで申し訳ないのですが、下記のコードで回収した場合、どのようにしたらよいでしょうか??
<select class="age" name="month01">
$max_month = 12;
$max_day = 31;
$this_month = date(m);
$this_day = date(d);
$mikomi_month = $this_month;
$mikomi_day = $this_day;
if($mikomi_day>$max_day) {
$mikomi_day -= $max_day;
$mikomi_month = $this_month+1;
if($this_month>$max_month) {
$mikomi_month = $this_month-$max_month;
}
}
for($i=1;$i<=$max_month;$i++) {
?>
<option value="<?php echo $i;?>月" <?php if($i==$mikomi_month){echo "selected=\"selected\"";}?> >
</option>
}
?>
</select>
月
<select class="age" name="day01">
for($i=1;$i<=$max_day;$i++) {
?>
<option value="<?php echo $i;?>日" <?php if($i==$mikomi_day){echo "selected=\"selected\"";}?> >
</option>
}
?>
</select>
日
<select class="age" name="oclock01" onblur="Validator.check(this)">
<option value="10:00~12:00" selected="selected">10:00~12:00</option>
<option value="12:00~13:30" >12:00~13:30</option>
<option value="15:00~17:00">15:00~17:00</option>
<option value="17:00~19:30">17:00~19:30</option>
<option value="何時でもOK">何時でもOK</option>
</select>