1 /** 2 * 年龄转生日(模糊结果) 3 * @parameter int age(年龄) 4 * @parameter string symbol(分隔符) 5 * @return string (yyyy*mm*dd) 6 * @author he 7 */ 8 private function agetobirthday($age,$symbol='-') 9 { 10 $age = $age==0?25:$age; 11 $nowyear = date("Y",time()); 12 $year = $nowyear-$age; 13 $monthArr = []; 14 for ($i=1;$i<13;$i++){ 15 $monthArr[] = $i<10?'0'.$i:$i; 16 } 17 $dayArr = []; 18 for ($i=1;$i<29;$i++){ 19 $dayArr[] = $i<10?'0'.$i:$i; 20 } 21 $month_key = array_rand($monthArr,1); 22 $month = $monthArr[$month_key]; 23 $date_tmp_stamp = strtotime($year.'-'.$month); 24 $day = ''; 25 if( $month=='02' && date("t",$date_tmp_stamp)=='29' ) { 26 $dayArr = array_merge($dayArr,['29']); 27 $day_key = array_rand($dayArr,1); 28 $day = $dayArr[$day_key]; 29 } else if ( $month=='02' && date("t",$date_tmp_stamp)=='28' ){ 30 $day_key = array_rand($dayArr,1); 31 $day = $dayArr[$day_key]; 32 } else if( in_array($month, ['01','03','05','07','08','10','12']) ) { 33 $dayArr = array_merge($dayArr,['29','30','31']); 34 $day_key = array_rand($dayArr,1); 35 $day = $dayArr[$day_key]; 36 } else { 37 $dayArr = array_merge($dayArr,['29','30']); 38 $day_key = array_rand($dayArr,1); 39 $day = $dayArr[$day_key]; 40 } 41 return $year.$symbol.$month.$symbol.$day; 42 } 43 44 /** 45 * 生日转年龄 46 * @author he 47 * @parameter birthday:yyyy-mm-dd 48 * @return str 49 */ 50 private function birthdaytoage($birthday) 51 { 52 $age = 0; 53 $year = date('Y',strtotime($birthday)); 54 $month = date('m',strtotime($birthday)); 55 $day = date('d',strtotime($birthday)); 56 57 $now_year = date('Y'); 58 $now_month = date('m'); 59 $now_day = date('d'); 60 61 if ($now_year > $year) { 62 $age = $now_year - $year - 1; 63 if ($now_month > $month) { 64 $age++; 65 } else if ($now_month == $month) { 66 if ($now_day >= $day) { 67 $age++; 68 } 69 } 70 } 71 if(empty($birthday)){ 72 $age = 26; 73 } 74 return $age; 75 } 76