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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog