php根据出生日期计算 年龄/生肖/星座

180it 2019-12-11 PM 2051℃ 0条

/**

  • 根据出生日期计算 年龄/生肖/星座
  • @param $birth 日期格式的年月日,例:'1999-11-15'
  • @param $symbol 用什么符号分割,例:'-'
  • @return $data 返回数据
    */

function birthday($birth, $symbol='-'){

// 把数组中的值赋给变量
list($birth_year, $birth_month, $birth_day) = explode($symbol, $birth);
// 计算年龄
$now_month = date('n'); // 当前月份,不带前导0
$now_day = date('j'); // 当前月的第几天,不带前导0
$age = date('Y') - $birth_year - 1;
if ($now_month > $birth_month || $now_month == $birth_month && $now_day > $birth_day) {
    $age++;
}
$data['age'] = $age; // 年龄
//计算生肖
$animals = array(
    '鼠', '牛', '虎', '兔', '龙', '蛇',
    '马', '羊', '猴', '鸡', '狗', '猪'
);
$key = ($birth_year - 1900) % 12;
$data['animals'] = $animals[$key]; // 生肖
//计算星座
$constellation_name = array(
    '水瓶座','双鱼座','白羊座','金牛座','双子座','巨蟹座',
    '狮子座','处女座','天秤座','天蝎座','射手座','摩羯座'
);
if ($birth_day <= 22){
    if ('1' !== $birth_month){
        $constellation = $constellation_name[$birth_month - 2];
    }else{
        $constellation = $constellation_name[11];
    }
}else{
    $constellation = $constellation_name[$birth_month - 1];
 }
$data['constellation'] = $constellation; // 星座
return $data; // 返回数据

}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

php根据出生日期计算 年龄/生肖/星座