Home  >  Article  >  Backend Development  >  Determine constellation and zodiac signs based on birthday in PHP_PHP tutorial

Determine constellation and zodiac signs based on birthday in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:471161browse

Judging constellations is very simple. We need to count all the dates and time periods of each week, and then get the dates for query. I will give you two examples below, students in need can refer to them.

Horoscope: I wrote this based on this timeline, which may not be accurate.

'Aquarius'=>'(1/22-2/21)', 'Pisces'=>'(2/22-3/21)',

'Aries'=>'(3/22-4/21)', 'Taurus'=>'(4/22-5/21)',

'Gemini'=>'(5/22-6/21)', 'Cancer'=>'(6/22-7/21)',

'Leo'=>'(7/22-8/21)', 'Virgo'=>'(8/22-9/21)',

'Libra'=>'(9/22-10/21)', 'Scorpio'=>'(10/22-11/21)',

'Sagittarius'=>'(11/22-12/21)', 'Capricorn'=>'(12/22-1/21)'


Determine the horoscope function based on the date

The code is as follows Copy code
 代码如下 复制代码

function yige_constellation($month, $day) {
 // 检查参数有效性
 if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false;
 
 // 星座名称以及开始日期
 $constellations = array(
  array( "20" => "宝瓶座"),
  array( "19" => "双鱼座"),
  array( "21" => "白羊座"),
  array( "20" => "金牛座"),
  array( "21" => "双子座"),
  array( "22" => "巨蟹座"),
  array( "23" => "狮子座"),
  array( "23" => "处女座"),
  array( "23" => "天秤座"),
  array( "24" => "天蝎座"),
  array( "22" => "射手座"),
  array( "22" => "摩羯座")
 );
 
 list($constellation_start, $constellation_name) = each($constellations[(int)$month-1]);
 
 if ($day < $constellation_start) list($constellation_start, $constellation_name) = each($constellations[($month -2 < 0) ? $month = 11: $month -= 2]);
 
 return $constellation_name;
}

function yige_constellation($month, $day) { // Check parameter validity if ($month < 1 || $month > 12 || $day < 1 || $day > 31) return false; // Constellation name and start date $constellations = array( array( "20" => "Aquarius"), array( "19" => "Pisces"), array( "21" => "Aries"), array( "20" => "Taurus"), array( "21" => "Gemini"), array( "22" => "Cancer"), array( "23" => "Leo"), array( "23" => "Virgo"), array( "23" => "Libra"), array( "24" => "Scorpio"), array( "22" => "Sagittarius"), array( "22" => "Capricorn") ); list($constellation_start, $constellation_name) = each($constellations[(int)$month-1]); if ($day < $constellation_start) list($constellation_start, $constellation_name) = each($constellations[($month -2 < 0) ? $month = 11: $month -= 2]);<🎜> <🎜> return $constellation_name;<🎜> }<🎜>

The following is more comprehensive and can directly check age, zodiac sign, and constellation based on birthday

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The code is as follows
代码如下 复制代码

/**

* 根据生日中的月份和日期来计算所属星座

*

* @param int $birth_month

* @param int $birth_date

* @return string

*/

function get_constellation($birth_month,$birth_date)

{

//判断的时候,为避免出现1和true的疑惑,或是判断语句始终为真的问题,这里统一处理成字符串形式

$birth_month = strval($birth_month);

$constellation_name = array(

'水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座',

'狮子座','处女座','天秤座','天蝎座)','射手座','摩羯座'

);

if ($birth_date <= 22)

{

if ('1' !== $birth_month)

{

$constellation = $constellation_name[$birth_month-2];

}

else

{

$constellation = $constellation_name[11];

}

}

else

{

$constellation = $constellation_name[$birth_month-1];

}

return $constellation;

}


/**

* 根据生日中的年份来计算所属生肖

*

* @param int $birth_year

* @return string

*/

function get_animal($birth_year)

{

//1900年是子鼠年

$animal = array(

'子鼠','丑牛','寅虎','卯兔','辰龙','巳蛇',

'午马','未羊','申猴','酉鸡','戌狗','亥猪'

);

$my_animal = ($birth_year-1900)%12;

return $animal[$my_animal];

}


/**

* 根据生日来计算年龄

*

* 用Unix时间戳计算是最准确的,但不太好处理1970年之前出生的情况

* 而且还要考虑闰年的问题,所以就暂时放弃这种方式的开发,保留思想

*

* @param int $birth_year

* @param int $birth_month

* @param int $birth_date

* @return int

*/

function get_age($birth_year,$birth_month,$birth_date)

{

$now_age = 1; //实际年龄,以出生时为1岁计

$full_age = 0; //周岁,该变量放着,根据具体情况可以随时修改

$now_year = date('Y',time());

$now_date_num = date('z',time()); //该年份中的第几天

$birth_date_num = date('z',mktime(0,0,0,$birth_month,$birth_date,$birth_year));

$difference = $now_date_num - $birth_date_num;

if ($difference > 0)

{

  $full_age = $now_year - $birth_year;

}

else

{

  $full_age = $now_year - $birth_year - 1;

}

 

$now_age = $full_age + 1;

 

return $now_age;

}


?>

Copy code

/**

* * @param int $birth_month * @param int $birth_date * @return string */
function get_constellation($birth_month,$birth_date)

{
//When judging, in order to avoid confusion between 1 and true, or the problem that the judgment statement is always true, it is unified into a string form <🎜> <🎜>$birth_month = strval($birth_month);<🎜> <🎜> <🎜> <🎜>$constellation_name = array(<🎜> <🎜> ‘Aquarius’, ‘Pisces’, ‘Aries’, ‘Taurus’, ‘Gemini’, ‘Cancer’, <🎜> <🎜> ‘Leo’, ‘Virgo’, ‘Libra’, ‘Scorpio’, ‘Sagittarius’, ‘Capricorn’ <🎜> <🎜> );<🎜> <🎜> <🎜> <🎜>if ($birth_date <= 22)<🎜> <🎜>{<🎜> <🎜> if ('1' !== $birth_month)<🎜> <🎜> {<🎜> <🎜> $constellation = $constellation_name[$birth_month-2];<🎜> <🎜> }<🎜> <🎜> else<🎜> <🎜> {<🎜> <🎜> $constellation = $constellation_name[11];<🎜> <🎜> }<🎜> <🎜>}<🎜> <🎜>else<🎜> <🎜>{<🎜> <🎜> $constellation = $constellation_name[$birth_month-1];<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>return $constellation;<🎜> <🎜>}<🎜> <🎜>
/**<🎜> <🎜>* Calculate your zodiac sign based on the year of your birthday<🎜> <🎜>*<🎜> <🎜>* @param int $birth_year<🎜> <🎜>* @return string<🎜> <🎜>*/<🎜> <🎜>function get_animal($birth_year)<🎜> <🎜>{<🎜> <🎜>//1900 is the Year of the Rat<🎜> <🎜>$animal = array(<🎜> <🎜> ‘Zi Shu’, ‘Chou Niu’, ‘Yin Hu’, ‘Mao Rabbit’, ‘Chen Long’, ‘Si Snake’, <🎜> <🎜> ‘Wuma’, ‘Weiyang’, ‘Shenmonkey’, ‘Yourooster’, ‘Xudog’, ‘Haizhu’<🎜> <🎜> );<🎜> <🎜> <🎜> <🎜>$my_animal = ($birth_year-1900)%12;<🎜> <🎜>return $animal[$my_animal];<🎜> <🎜>}<🎜> <🎜>
/**<🎜> <🎜>* Calculate age based on birthday<🎜> <🎜>*<🎜> <🎜>* Using Unix timestamp calculation is the most accurate, but it is not easy to handle cases born before 1970<🎜> <🎜>* We also have to consider the issue of leap years, so we will temporarily abandon this method of development and keep our thoughts <🎜> <🎜>*<🎜> <🎜>* @param int $birth_year<🎜> <🎜>* @param int $birth_month<🎜> <🎜>* @param int $birth_date<🎜> <🎜>* @return int<🎜> <🎜>*/<🎜> <🎜>function get_age($birth_year,$birth_month,$birth_date)<🎜> <🎜>{<🎜> <🎜>$now_age = 1; //Actual age, calculated as 1 year old at birth<🎜> <🎜>$full_age = 0; //One year old, this variable is stored and can be modified at any time according to the specific situation<🎜> <🎜> <🎜> <🎜>$now_year = date('Y',time());<🎜> <🎜>$now_date_num = date('z',time()); //The day of the year<🎜> <🎜>$birth_date_num = date('z',mktime(0,0,0,$birth_month,$birth_date,$birth_year));<🎜> <🎜> <🎜> <🎜>$difference = $now_date_num - $birth_date_num;<🎜> <🎜>if ($difference > 0) { $full_age = $now_year - $birth_year; } else { $full_age = $now_year - $birth_year - 1; } $now_age = $full_age + 1; return $now_age; }
?> http://www.bkjia.com/PHPjc/628740.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628740.htmlTechArticleJudging constellations is very simple. We need to count all the date and time periods of each week, and then get the date for query. Okay, let me give you two examples below. Students in need can refer to it...