没事干啊,不想复习啊,学不进去啊,不知道学什么啊,上周就是把项目完善了下,也没用到什么新知识,也没什么要记录的方法,都是老黄讲过的,前端真的不想做,太累太烦,贴段代码完事!
<?php
function calTime(){
//验证是否登录
if(isset($_SESSION['user_id'])){
$userName = $_SESSION['user_name'];
$searchName = isset($_REQUEST['search']) ? $_REQUEST['search'] :"";
$year = isset($_REQUEST['year']) ? $_REQUEST['year'] :"";
$month = isset($_REQUEST['month']) ? $_REQUEST['month'] :"";
$day = isset($_REQUEST['day']) ? $_REQUEST['day'] :"";
$height = isset($_REQUEST['height']) ? $_REQUEST['height'] :"";
$weight = isset($_REQUEST['weight']) ? $_REQUEST['weight'] :"";
$sex = isset($_REQUEST['sex']) ? $_REQUEST['sex'] :"";
$smoke = isset($_REQUEST['smoke']) ? $_REQUEST['smoke'] :"";
$drink = isset($_REQUEST['drink']) ? $_REQUEST['drink'] :"";
$character = isset($_REQUEST['character']) ? $_REQUEST['character'] :"";
//验证数据是否填写完整
if($userName && $searchName && $year && $month && $day && $height && $weight && $smoke && $drink && $character ){
//随机出一个人的死亡日期
$deadYear = array();
for($i=65;$i<80;$i++){
$deadYear[] = $i;
}
$deadMonth =array();
for($j=1;$j<13;$j++){
$deadMonth[] = $j;
}
$deadDay =array();
for($x=1;$x<28;$x++){
$deadDay[] = $x;
}
$age = $deadYear[array_rand($deadYear,1)];
$BMI1 = $weight / (($height/100) * ($height/100));
$BMI = sprintf("%.1f" , $BMI1);
//根据所填写的数据减少或增加寿命
if($smoke == "yes"){
$age = 60;
}else{
$age += 3;
}
if($drink == "yes"){
$age -= 5;
}else{
$age += 3;
}
if($sex == "man"){
$age -= 3;
}else{
$age += 3;
}
if($character == "normal"){
$age += 1;
}else if($character == "pessimistic"){
$age -= 5;
}else{
$age += 5;
}
if($BMI<18.5){
$age-=3;
}else if($BMI>18.5 && $BMI<24.99){
$age+=3;
}else if($BMI>25 && $BMI<28){
$age-=3;
}else if($BMI>28 && $BMI<32){
$age-=5;
}else{
$age-=10;
}
$deadTime = ($year + $age)."-".$deadMonth[array_rand($deadMonth,1)].'-'.$deadDay[array_rand($deadDay,1)];
$distance = date_to_unixtime($deadTime)-time();
$con = connect( "localhost","root","123456","dead");
$sql = "select * from main;";
$query = query( $sql , $con );
$result = fetch_all( $query );
for($z=0;$z<count($result);$z++){
if($result[$z]["user_name"]==$userName && $result[$z]["search_name"]==$searchName && $result[$z]["user_year"]==$year && $result[$z]["user_month"]==$month && $result[$z]["user_day"]==$day && $result[$z]["user_BMI"]==$BMI && $result[$z]["user_sex"]==$sex && $result[$z]["is_smoke"]==$smoke && $result[$z]["is_wine"]==$drink && $result[$z]["user_character"]==$character){
echo json_encode(array("deadTime" => $result[$z]["user_dead"] , "distance" => (date_to_unixtime($result[$z]["user_dead"])-time())));
break;
}else{
if($z == (count($result)-1)){
$sql = "insert into main(user_name,search_name,user_year,user_month,user_day,user_BMI,user_sex,is_smoke,is_wine,user_character,user_alive,user_dead,user_distance) values('".$userName."','".$searchName."','".$year."','".$month."','".$day."','".$BMI."','".$sex."','".$smoke."','".$drink."','".$character."','".$age."','".$deadTime."','".$distance."');";
query( $sql, $con );
echo json_encode(array("deadTime" => $deadTime , "distance" => $distance));
}
continue;
}
}
}else{
echo json_encode(array("code"=>"-1","message"=>"请填写完整的数据"));
}
}else{
echo json_encode(array("code"=>"-2","message"=>"请先登录"));
}
}
//时间转化
function date_to_unixtime($date, $timezone = 'PRC') {
$datetime= new DateTime($date, new DateTimeZone($timezone));
return $datetime->format('U');
}
?>