/** * 修复 a表 生日格式问题 * @author qin */ public function update_birthday_one() { $this->load->model('a'); $datearr = @addslashes_array($this->input->get()); $page_cur = empty($datearr['page'])?0:$datearr['page']; $nextpage = $page_cur+1; $page_num = 5000; // 5000左右比较好 $limitmin = $page_num * $page_cur; $limitsql = "limit $limitmin,$page_num"; // 查出xw_baoxian_spread_baoxian的生日 $sql = "select id,birthday,age from a where age=0".$limitsql; $birth_data2 = $this->a->getwhere2($sql); if(empty($birth_data2['data'])){ echo 'OVER'; exit; } foreach ($birth_data2['data'] as $key => $val) { $id = $val['id']; $birthday = $val['birthday']; $age = $val['age'];// 去除生日中的-和/符号 $birthday = str_replace('/', '', str_replace('-', '', $birthday)); // 把生日转换成时间戳 $birthday_in = 0; if(!empty($birthday)){ $birthday_in = date('Y-m-d',strtotime($birthday)); } //获取年龄 $age= $this->birthdaytoage($birthday_in); // 更新生日和生日的时间戳 $this->a->update($id, ['age' => $age]); } $url = "https://xxx.com/test/update_birthday_one.html?page=$nextpage"; echo '<script>url="'.$url.'";window.location.href=url;</script>'; }
/** * 年龄转生日(模糊结果) * @parameter int age(年龄) * @parameter string symbol(分隔符) * @return string (yyyy*mm*dd) */ private function agetobirthday($age,$symbol='-') { $age = $age==0?25:$age; $nowyear = date("Y",time()); $year = $nowyear-$age; $monthArr = []; for ($i=1;$i<13;$i++){ $monthArr[] = $i<10?'0'.$i:$i; } $dayArr = []; for ($i=1;$i<29;$i++){ $dayArr[] = $i<10?'0'.$i:$i; } $month_key = array_rand($monthArr,1); $month = $monthArr[$month_key]; $date_tmp_stamp = strtotime($year.'-'.$month); $day = ''; if( $month=='02' && date("t",$date_tmp_stamp)=='29' ) { $dayArr = array_merge($dayArr,['29']); $day_key = array_rand($dayArr,1); $day = $dayArr[$day_key]; } else if ( $month=='02' && date("t",$date_tmp_stamp)=='28' ){ $day_key = array_rand($dayArr,1); $day = $dayArr[$day_key]; } else if( in_array($month, ['01','03','05','07','08','10','12']) ) { $dayArr = array_merge($dayArr,['29','30','31']); $day_key = array_rand($dayArr,1); $day = $dayArr[$day_key]; } else { $dayArr = array_merge($dayArr,['29','30']); $day_key = array_rand($dayArr,1); $day = $dayArr[$day_key]; } return $year.$symbol.$month.$symbol.$day; } /** * 生日转年龄 * @parameter birthday:yyyy-mm-dd * @return str` */ private function birthdaytoage($birthday) { $age = 0; $year = date('Y',strtotime($birthday)); $month = date('m',strtotime($birthday)); $day = date('d',strtotime($birthday)); $now_year = date('Y'); $now_month = date('m'); $now_day = date('d'); if ($now_year > $year) { $age = $now_year - $year - 1; if ($now_month > $month) { $age++; } else if ($now_month == $month) { if ($now_day >= $day) { $age++; } } } if(empty($birthday)){ $age = 26; } return $age; }