doubiao7267 2017-04-09 18:21
浏览 59
已采纳

如何从PHP中的数组+打印名称获取最接近的生日日期

Started to use PHP about 2 weeks ago,got first problems =D

Can't figure how to get my task done.

Program have to give next upcoming birthday and name.Thanks in advance

How to make this programm print out something like :

Jessy Pinkmann 2017-09-11

<?php

$today=new DateTime();


$Bdays = array
(
    array("Lucienne Adkisson",date_create("2017-10-17")),            //0    //  Y/M/D
    array("Sheryll Nestle",date_create("2017-02-16")),            //1
    array("Tim Pittman",date_create("2017-07-29")),           //2
    array("Elmer Letts",date_create("2017-12-01")),                 //3
    array("Gino Massengale",date_create("2017-04-16")),              //4
    array("Jeremy Wiggs ",date_create("2017-09-17")),            //5
    array("Julian Bulloch ",date_create("2017-06 -21")),             //6
    array("Joella Hinshaw  ",date_create("2017-06-25")),                //7
    array("Mamie Burchill  ",date_create("2017-11-15")),                    //8
    array("Constance Segers  ",date_create("2017-06-30")),                //9
    array("Jessy Pinkmann",date_create("2017-09-11")),             //10
    array("Dudley Currie",date_create("2017-02-10"))             //11
);


for ($x =0; $x <= 11; $x++ ){


$diff=date_diff($today,$Bdays[$x][1]);
echo $diff->format("  %R%a");

}

?>
  • 写回答

4条回答 默认 最新

  • dongli1920 2017-04-09 19:26
    关注

    You can try this code:

    <?php
    $today=new DateTime();
    $count = 0;
    
    
    $Bdays = array(
        array("Lucienne Adkisson",date_create("2017-10-17")),            
        array("Sheryll Nestle",date_create("2017-02-16")),            
        array("Tim Pittman",date_create("2017-07-29")),           
        array("Elmer Letts",date_create("2017-12-01")),                 
        array("Gino Massengale",date_create("2017-04-16")),              
        array("Jeremy Wiggs ",date_create("2017-09-17")),            
        array("Julian Bulloch ",date_create("2017-06 -21")),             
        array("Joella Hinshaw  ",date_create("2017-06-25")),               
        array("Mamie Burchill  ",date_create("2017-11-15")),                   
        array("Constance Segers  ",date_create("2017-06-30")),                
        array("Jessy Pinkmann",date_create("2017-09-11")),             
        array("Dudley Currie",date_create("2017-02-10"))             
    );
    
    
    
    for($x = 0, $len = count($Bdays); $x < $len; $x++){
    
        $diff = date_diff($today, $Bdays[$x][1]);
       
        $difference = $diff->format("  %R%a");
        if($difference > 0){
            if($count == 0){
                $smallest_diff = $difference;
            }
            $count++;
            if($difference < $smallest_diff){
                $smallest_diff = $difference;
                $closest_index = $x;
            }
        }
    }
    
    echo "The upcoming birtday person is: <b>" . $Bdays[$closest_index][0] . '</b> and his birthday is on: <b>' . date_format($Bdays[$closest_index][1], "Y/m/d") . '</b>';
    
    ?>

    It's woking fine. Here is the output:

    enter image description here

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数