phpな休日 BBS sitemap
1. 今年もあと ×× 日で終わりですね。

date と strtotime 関数で遊んでみます。

<?php
$time = date(Y . "-" . m . "-" . d);
$final = date(Y) . "-12-31";
$days = (strtotime($final)-strtotime($time))/(60*60*24)+1;
print "今年もあと(今日を含めて) " . $days . " 日で終わりですね。";
?>

実行

逆のパターンも。(今年もすでに ×× 日が過ぎ去りました)

<?php
$time = date(Y . "-" . m . "-" . d);
$start = date(Y) . "-01-01";
$days = (strtotime($time)-strtotime($start))/(60*60*24)+1;
print "今年もすでに(今日を含めて) " . $days . " 日が過ぎ去りました。";
?>

実行

生まれてから(誕生日から)何日経ったかを計算します。
フォームからの送信を受ける時に正規表現での書式チェックを行います。

<?php
if ((isset($_POST["birthy"])) or (isset($_POST["birthm"])) or (isset($_POST["birthd"]))) { //POSTされてきた場合
if (isset($_POST["birthy"])) {$birthy = $_POST["birthy"];} //年
if (isset($_POST["birthm"])) {$birthm = $_POST["birthm"];} //月
if (isset($_POST["birthd"])) {$birthd = $_POST["birthd"];} //日
$birthday = $birthy . "-" . $birthm . "-" . $birthd;
if (preg_match("|^¥d{4}-¥d{2}-¥d{2}$|", $birthday)) { //正規表現で桁のチェック
if (checkdate($birthm, $birthd, $birthy) == false) { //ありえない日付だったら
print $birthy . "年" . $birthm . "月" . $birthd . "日はありえない日付です。";
}
else {
$time = date(Y . "-" . m . "-" . d); //現在の年月日
$days = (strtotime($time)-strtotime($birthday))/(60*60*24)+1; //引き算
print $birthy . "年" . $birthm . "月" . $birthd . "日生  ";
print "誕生から(今日を含めて) " . $days . " 日経ちます。";
}
}
else {
print "入力が間違っています。生年月日を 0000 00 00 (半角)の書式で入力してください。(例:1960年1月1日なら 1960 01 01 )";
}
}
else {
print "生年月日を 0000 00 00 (半角)の書式で入力してください。(例:1960年1月1日なら 1960 01 01 )";
}
?>
<form method="POST" action="">
<input type="text" name="birthy" size="5"> 年 <input type="text" name="birthm" size="3"> 月 <input type="text" name="birthd" size="3"> 日生まれ <input type="submit" value="送信">
</form>
<?php
if ($days > 10000) {
print "<p class='txt'>¥n";
print "けっこうとんでもない日数を生きているものですが、一日一日を大切にしましょう。¥n";
print "</p>¥n";
}
?>

実行
2. 生年月日から年齢を求める

「今年何歳になります」 だけなら簡単なんですけどね。 [現在年]-[生まれた年]= で良いので。
問題は今日が今年の誕生日前なのか後(あるいは当日)なのかってことで1つ違ってくるところ。
生まれた日の月日と今日の月日を比べて生まれた日の数値が大きかったら、まだ誕生日前ということですので-1します。

その比較にも関係しますが、入力ミスを防ぐ意味で今回はフォームに1桁の数字を入力しても、受けたときに2桁に修正する(sprintfで0を付加する)処理を加えています。

<?php
if ((isset($_POST["birthy"])) or (isset($_POST["birthm"])) or (isset($_POST["birthd"]))) { //POSTされてきた場合
if (isset($_POST["birthy"])) {$birthy = $_POST["birthy"];}
if (isset($_POST["birthm"])) {$birthm = $_POST["birthm"];}
if (isset($_POST["birthd"])) {$birthd = $_POST["birthd"];}
$birthm = sprintf("%02d", $birthm); //1桁で来たものを2桁にする
$birthd = sprintf("%02d", $birthd); //1桁で来たものを2桁にする
$birthday = $birthy . "-" . $birthm . "-" . $birthd;
if (checkdate($birthm, $birthd, $birthy) == false) { //ありえない日付だったら
print $birthy . "年" . $birthm . "月" . $birthd . "日はありえない日付です。";
}
else {
$nowy = date("Y"); //現在年
$nowm = date("m"); //現在月
$nowd = date("d"); //現在日
$birthmd = $birthm . $birthd;
$nowmd = $nowm . $nowd;
$age = $nowy - $birthy;
if ($birthmd > $nowmd)
$age = $age-1;
print "" . $birthy . "年" . $birthm . "月" . $birthd . "日生まれの方は今日(" . $nowy . "年" . $nowm . "月" . $nowd . "日)現在" . $age . "歳です。¥n";
}
}
else {
print "生年月日を 0000 00 00 (半角)の書式で入力してください。(例:1960年1月1日なら 1960 1 1 )";
}
?>
<form method="POST" action="">
<input type="text" name="birthy" size="5"> 年 <input type="text" name="birthm" size="3"> 月 <input type="text" name="birthd" size="3"> 日生まれ <input type="submit" value="送信">
</form>

実行
3. 和暦に対応する

生年月日といえば和暦を使用しますので、和暦で入力して計算は西暦で実行させるようにしたいもの。
和暦→西暦変換の準備としてこのような テキストファイルを用意します。
フォームのプルダウン表示で外部テキストを読み込み、POSTで受け取った和暦を同じテキストファイルから西暦を取得します。

<?php
if ((isset($_POST["japan"])) or (isset($_POST["birthm"])) or (isset($_POST["birthd"]))) { //POSTされてきた場合
if (isset($_POST["japan"])) {$japan = $_POST["japan"]; //$_POST["japan"] を $japan に
$ydata = file("./year.cgi"); //データをオープン
for($i=0; $i<count($ydata); $i++) { //1行づつ走査
$list = explode("<>",$ydata[$i]); //<>で区切る
if ($list[0] == $japan) { //$japan を探す
$birthy = $list[1]; //西暦を $birthy に
break; //処理を終了
}
}
}
if (isset($_POST["birthm"])) {$birthm = $_POST["birthm"];}
if (isset($_POST["birthd"])) {$birthd = $_POST["birthd"];}
$birthm = sprintf("%02d", $birthm); //1桁で来たものを2桁にする
$birthd = sprintf("%02d", $birthd); //1桁で来たものを2桁にする
$birthday = $birthy . "-" . $birthm . "-" . $birthd;
if (checkdate($birthm, $birthd, $birthy) == false) { //ありえない日付だったら
print $japan . "年(" . $birthy . "年)" . $birthm . "月" . $birthd . "日はありえない日付です。";
}
else {
$nowy = date("Y"); //現在年
$ydata = file("./year.cgi"); //データをオープン
for($i=0; $i<count($ydata); $i++) { //1行づつ走査
$list = explode("<>",$ydata[$i]); //<>で区切る
if ($list[1] == $nowy) { //$nowy を探す
$nowj = $list[0]; //和暦を $nowj に
break; //処理を終了
}
}
$nowm = date("m"); //現在月
$nowd = date("d"); //現在日
$birthmd = $birthm . $birthd;
$nowmd = $nowm . $nowd;
$age = $nowy - $birthy;
if ($birthmd > $nowmd)
$age = $age-1;
print $japan . "年(" . $birthy . "年)" . $birthm . "月" . $birthd . "日生まれの方は今日(" . $nowj . "年(" . $nowy . "年)" . $nowm . "月" . $nowd . "日)現在" . $age . "歳です。¥n";
}
}
else {
print "生年月日を入力してください。";
}
?>
<form method="POST" action="">
<select name="japan">
<option <?php if ((!$birthy) or ($birthy == "和暦")) {print "selected";} ?>>和暦</option>
<?php
$data = array();
$vdata = fopen("./year.cgi", "r"); //データを読み込む
while (!feof($vdata)) $data[] = fgets($vdata);
array_pop($data);
fclose($vdata); //ファイルを閉じる
foreach ($data as $data) {
$scan = explode("<>", $data);
print "" . "<option ";
if ($calen == $scan[0]) {
print "selected";
}
else {
print "value='" . $scan[0] . "'";
}
print ">" . $scan[0] . "</option><br>¥n";
}
?>
</select>
年 <input type="text" name="birthm" size="3"> 月 <input type="text" name="birthd" size="3"> 日生まれ <input type="submit" value="送信">
</form>

実行

Fatal error: Uncaught Error: Undefined constant "Y.n.j" in /home/users/2/secret.jp-final/web/phpholiday/34/index.cgi:212 Stack trace: #0 /home/users/2/secret.jp-final/web/phpholiday/index.php(334): include() #1 {main} thrown in /home/users/2/secret.jp-final/web/phpholiday/34/index.cgi on line 212