本文介绍了用php获取下一个生日的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个json,用于存储生日数据.

I have a json which storing the data of birthdays.

    {
    "user": [
          {
    "id": "1",
    "name": "Tom",
    "bday": {
          "month": "1",
          "day": "23"
          }
    },
    {
    "id": "2",
    "name": "Tommy",
    "bday": {
          "month": "1",
          "day": "28"
          }
    },
    {
    "id": "3",
    "name": "Mary",
    "bday": {
          "month": "1",
          "day": "30"
          }
    },
    {
    "id": "4",
    "name": "Kelvin",
    "bday": {
          "month": "1",
          "day": "1"
          }
    },
    {
    "id": "5",
    "name": "Peter",
    "bday": {
          "month": "1",
          "day": "1"
          }
    }
    ]
    }

我想要一个函数返回今天或下一个生日的用户.例如,今天是1月21日(01-21),它将返回汤姆(01-23),如果今天是1月1日,它将返回Peter和Kelvin(01-01).

I want a function return the users which are birthday today or the next.For example , today is 21 Jan (01-21) , it will return Tom (01-23), If today is 1 Jan , it will return Peter and Kelvin (01-01).

这是我的主意,但我不知道如何在php中执行此操作...而且我认为这不是实现此目的的最佳解决方案.有更好的主意吗?

Here is my idea but i dont know how to do this in php...And I think this is not the best solution to do this. Any better idea?

function findBday($today){
$month = today's month;
$day = today's day;

//make a loop
if($month == $bday[month] && $day == $bday[day]){
// put the result into array
} else {
//put $month - $bday[month] , $day - $bday[day]  and add the smallest one into array 
and sort them;
//if today is December and no one birthday in this month , find January 
  }
return $array;
}

推荐答案

希望这对您有所帮助.我的小程序具有与您描述的相同的逻辑:

Hope this will help you. I have appleid same logic you have describe for:

$json = '{
    "user": [
          {
    "id": "1",
    "name": "Tom",
    "bday": {
          "month": "1",
          "day": "23"
          }
    },
    {
    "id": "2",
    "name": "Tommy",
    "bday": {
          "month": "1",
          "day": "28"
          }
    },
    {
    "id": "3",
    "name": "Mary",
    "bday": {
          "month": "1",
          "day": "30"
          }
    },
    {
    "id": "4",
    "name": "Kelvin",
    "bday": {
          "month": "1",
          "day": "1"
          }
    },
    {
    "id": "5",
    "name": "Peter",
    "bday": {
          "month": "1",
          "day": "1"
          }
    }

    ]
    }';
$birthday_array = json_decode($json,true);
//var_dump($bir_array);
$result = findBday($birthday_array['user']);
var_dump($result);


function findBday($user_details){
    $this_month = date("n");
    $this_day = date("j");
    $return_array = array();
    $tmp_array = array();
    //make a loop
    foreach ($user_details as $user_detail) {        
        $bday = $user_detail['bday'];
        if($this_month == $bday['month'] && $this_day == $bday['day']){
            $return_array[] = $user_detail['name'] . "(".$bday['day']."-".$bday['month'].")";
        } 
        else {
            $tmp_array[] =  $user_detail;
          }
    }
    if(count($return_array) === 0 && count($tmp_array) > 0)
    {

        //sort using php usort
        usort ($tmp_array, 'sortByOrder'); 
        $closest = null;        
        $tmp_val = "";
        foreach ($tmp_array as $key => $value) {
            $month = $value['bday']['month'];
            $day = $value['bday']['day'];
            if($month == $this_month)
            {
                if($day > $this_day){
                    $tmp_val = $value;
                    break;
                }
            }
            elseif ($month > $this_month) {
                $tmp_val = $value;
                break;
            }
        }      
        if($tmp_val != "")
            $tmp_array = $tmp_val;
        else    
            $tmp_array = $tmp_array[0];
        $bday = $tmp_array['bday'];
        $return_array[] = $tmp_array['name'] . "(".$bday['day']."-".$bday['month'].")";

    }


    return $return_array;
}
function sortByOrder($a, $b) {    
    $day_a = $a['bday'];
    $day_b = $b['bday'];
    if ($day_a['month'] > $day_b['month'])
        return 1;
    elseif($day_a['month'] == $day_b['month'])
    {
        if ($day_a['day'] > $day_b['day'])   
            return 1;
    }

}

这篇关于用php获取下一个生日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 04:54