開発環境
- OS X Lion - Apple(OS)
- Apache (Web Server)
- PHP (サーバーサイドプログラミング言語、スクリプト言語)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
『初めてのPHP5』 (David Sklar 著、 桑村 潤 翻訳、 廣川 類 翻訳、 オライリー・ジャパン、2005年、ISBN978-4-87311-257-2)の9章(日付と時刻の取り扱い)9.6(演習問題)4を解いてみる。
4.
HTML、PHPのソースコード(BBEdit)
<?php
require 'formhelpers.php';
$time = mktime(0,0,0,12,32,2000);
$years = array();
for($year = date('Y'); $year < date('Y') + 100; $year++){
$years[$year] = $year;
}
$months = array( 1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr",
5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug",
9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec");
$days = array();
for($day = 1; $day <= 31; $day++){
$days[$day] = $day;
}
if(array_key_exists('_submit_check', $_POST)){
process_form();
} else {
show_form();
}
function show_form(){
global $years, $months, $days;
if(array_key_exists('_submit_check', $_POST)){
$defaults = $_POST;
} else {
$timestamp = strtotime('+1 month');
$defaults = array('year' => date('Y', $timestamp),
'month' => date('m', $timestamp),
'day' => date('d', $timestamp));
}
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '" >';
print "Year: ";
input_select("year", $defaults, $years);
print "Month: ";
input_select("month", $defaults, $months);
print "Day: ";
input_select("day", $defaults, $days);
input_submit('submit', 'submit');
print '<input type="hidden" name="_submit_check" value="1" />';
print '</form>';
}
function process_form(){
$future = mktime(0,0,0,$_POST['month'], $_POST['day'], $_POST['year']);
$tuesday = strtotime('tuesday');
print '今日から' . date('Y-m-d', $future) . 'までの火曜日の日付一覧<br />';
while($tuesday <= $future){
print date('Y-m-d', $tuesday) . '<br />';
$tuesday = strtotime('+1 week', $tuesday);
}
print '<a href="' . $_SERVER['PHP_SELF'] . '">戻る</a>';
}
?>
0 コメント:
コメントを投稿