星座判斷很簡單我們要統計出每個星期所有日期時間段了,然后獲取日期進行查詢即可了,下面我給大家舉兩個實例,有需要的同學可參考.
星座:我是根據這個時間表寫的,該時間表未必準確.
根據日期判斷星座函數
實例代碼如下:
- 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;
- }
下面這個更全面可直接根據生日檢查年齡,生肖,星座
實例代碼如下:
- <?php
- /**
- * 根據生日中的月份和日期來計算所屬星座
- *
- * @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;
- }
- ?>
新聞熱點
疑難解答