PHP输入年份查询属相的代码。
我是新手,还不太懂,我写的这段有没有更简短的方法代替呢。<?php$year=$_GET['year'];$a=($year-1987)%12;$b=($year-198...
我是新手,还不太懂,我写的这段有没有更简短的方法代替呢。
<?php
$year = $_GET['year'];
$a = ($year-1987)%12;
$b = ($year-1988)%12;
$c = ($year-1989)%12;
$d = ($year-1990)%12;
$e = ($year-1991)%12;
$f = ($year-1992)%12;
$g = ($year-1993)%12;
$h = ($year-1994)%12;
$i = ($year-1995)%12;
$j = ($year-1996)%12;
$k = ($year-1997)%12;
$l = ($year-1998)%12;
if($year == ""){
echo "请输入正确的年份,如 <font color=red>1987</font>";
}elseif($a == 0){
echo $year . "年是兔年.";
}elseif($b == 0){
echo $year . "年是龙年.";
}elseif($c == 0){
echo $year . "年是蛇年.";
}elseif($d == 0){
echo $year . "年是马年.";
}elseif($e == 0){
echo $year . "年是羊年.";
}elseif($f == 0){
echo $year . "年是猴年.";
}elseif($g == 0){
echo $year . "年是鸡年.";
}elseif($h == 0){
echo $year . "年是狗年.";
}elseif($i == 0){
echo $year . "年是猪年.";
}elseif($j == 0){
echo $year . "年是鼠年.";
}elseif($k == 0){
echo $year . "年是牛年.";
}elseif($l == 0){
echo $year . "年是虎年.";
}
?>
<form id="form1" name="form1" method="GET" action="phpyear.php">
输入年份查询属相:
<input type="text" name="year" size="8" maxlength="4"/>
<input type="submit" value="提交" />
</form> 展开
<?php
$year = $_GET['year'];
$a = ($year-1987)%12;
$b = ($year-1988)%12;
$c = ($year-1989)%12;
$d = ($year-1990)%12;
$e = ($year-1991)%12;
$f = ($year-1992)%12;
$g = ($year-1993)%12;
$h = ($year-1994)%12;
$i = ($year-1995)%12;
$j = ($year-1996)%12;
$k = ($year-1997)%12;
$l = ($year-1998)%12;
if($year == ""){
echo "请输入正确的年份,如 <font color=red>1987</font>";
}elseif($a == 0){
echo $year . "年是兔年.";
}elseif($b == 0){
echo $year . "年是龙年.";
}elseif($c == 0){
echo $year . "年是蛇年.";
}elseif($d == 0){
echo $year . "年是马年.";
}elseif($e == 0){
echo $year . "年是羊年.";
}elseif($f == 0){
echo $year . "年是猴年.";
}elseif($g == 0){
echo $year . "年是鸡年.";
}elseif($h == 0){
echo $year . "年是狗年.";
}elseif($i == 0){
echo $year . "年是猪年.";
}elseif($j == 0){
echo $year . "年是鼠年.";
}elseif($k == 0){
echo $year . "年是牛年.";
}elseif($l == 0){
echo $year . "年是虎年.";
}
?>
<form id="form1" name="form1" method="GET" action="phpyear.php">
输入年份查询属相:
<input type="text" name="year" size="8" maxlength="4"/>
<input type="submit" value="提交" />
</form> 展开
展开全部
PHP通过年份查询属相示例代码如下,具体逻辑体现在代码中。
<?php
//判断是否为日期格式,默认时间格式为Y-m-d
function is_date($dateStr,$fmt="Y-m-d"){
$dateArr = explode("-",$dateStr);
if(empty($dateArr)){
return false;
}
foreach($dateArr as $val){
if(strlen($val)<2){
$val="0".$val;
}
$newArr[]=$val;
}
$dateStr =implode("-",$newArr);
$unixTime=strtotime($dateStr);
$checkDate= date($fmt,$unixTime);
if($checkDate==$dateStr)
return true;
else
return false;
}
//通过出生年月获取属相
function getShuXiang($bithdayDate){
//判断输入日期格式
if(!is_date($bithdayDate)){
echo "日期输入错误,请检查!";
}
//1900年是鼠年
$data = array('鼠','牛','虎','兔','龙','蛇','马','羊','猴','鸡','狗','猪');
$index = ($bithdayDate-1900)%12;
return $data[$index];
}
echo "属相:".getShuXiang("1989-05-19");
//属相:蛇
?>
展开全部
<?php
if($_POST['year']){
$y = $_POST['year'];
$sxdict = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
$result['sx'] = $sxdict[(($y-4)%12)];
var_dump($result['sx']);
}
?>
<form action="a.php" method="post">
<input type="text" name="year" />
<input type="submit" >
</form>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$num = ($year-1987)%12;
switch ($num){
case 0:
echo "兔";break;
case 1:
echo "龙";break;
.
.
.
.
.
.
case 12:
echo "虎";break;
}
改成这样的能省不少代码量吧!
switch ($num){
case 0:
echo "兔";break;
case 1:
echo "龙";break;
.
.
.
.
.
.
case 12:
echo "虎";break;
}
改成这样的能省不少代码量吧!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
<?php
if($_POST['year']){
$y = $_POST['year'];
$sxdict = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
$result['sx'] = $sxdict[(($y-4)%12)];
var_dump($result['sx']);
}
?><form action="a.php" method="post">
<input type="text" name="year" />
<input type="submit" >
</form>
if($_POST['year']){
$y = $_POST['year'];
$sxdict = array('鼠', '牛', '虎', '兔', '龙', '蛇', '马', '羊', '猴', '鸡', '狗', '猪');
$result['sx'] = $sxdict[(($y-4)%12)];
var_dump($result['sx']);
}
?><form action="a.php" method="post">
<input type="text" name="year" />
<input type="submit" >
</form>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询