1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

誕生日から現在の学年(小学校)を求める

Posted at
grade.php
<?php
 
$birth = "20040501";
echo grade($birth);
 
//誕生日から現在の学年(小学校)を求める 小学生ではない場合:歳
function grade($birth){

//今日の日付を取得
$now = date('Ymd');

//年齢の計算
$age = floor(($now-$birth)/10000);

//各月日を求める
$b_y = substr($birth, 0, 4);
$b_m = substr($birth, 4, 4);
$n_y = substr($now, 0, 4);
$n_m = substr($now, 4, 4);

if ($n_m < 400) { //前学期
	$m = 6;
} else { //新学期
	$m = 5;
}

if($b_m < 402) { //早生まれ
	$n_y++;
}

//学年の計算
$grade = $n_y - $b_y - $m;

//結果を返す
if($grade>=1 && $grade<=6){
	$data = $grade."年生";
}else{
	$data = $age."歳";
}

return $data;

}

?>
 
<html>
	<head>
		<meta charset="utf-8">
	</head>
</html>
1
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?