繁体   English   中英

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

[英]How to get closest birthday date from array + print name in PHP

大约2周前开始使用PHP,遇到了第一个问题= D

想不通如何完成任务。

程序必须提供下一个即将到来的生日和名字。

如何使该程序打印出类似以下内容的内容:

杰西·平克曼(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");

}

?>

我已经重组了一些代码。

<?php
$today = date("Y-m-d");

$Bdays = array
(
    array("Lucienne Adkisson", "2017-10-17"),
    array("Sheryll Nestle", "2017-02-16"),
    array("Tim Pittman", "2017-07-29"),
    array("Elmer Letts", "2017-12-01"),
    array("Gino Massengale", "2017-04-16"),
    array("Jeremy Wiggs", "2017-09-17"),
    array("Julian Bulloch", "2017-06 -21"),
    array("Joella Hinshaw", "2017-06-25"),
    array("Mamie Burchill", "2017-11-15"),
    array("Constance Segers", "2017-06-30"),
    array("Jessy Pinkmann", "2017-09-11"),
    array("Dudley Currie", "2017-02-10")
);


$tmp = 366;
$closest = ["", 0];
for ($i = 0; $i < count($Bdays); $i++)
{
    if (abs(strtotime($today) - strtotime($Bdays[$i][1])) / 86400 < $tmp)
    {
        $tmp = abs(strtotime($today) - strtotime($Bdays[$i][1])) / 86400;
        $closest = [$Bdays[$i][0], $Bdays[$i][1]];
    }
}
?>

$tmp设置为无值,为366天。 这是将存储与当前日期最接近的日期的变量。 我还更改了for循环以使用count函数 ,这意味着,如果向$Bdays添加更多日期,则它将为for循环迭代更多次。

$closest存储最近的日期的名称和生日。

abs函数会将负数转换为正数。 这是因为,例如,如果差异为-138 ,即使那意味着日期相距100天,这也将小于38 ,这仅仅是因为负数较低。

更新

我已经更新了代码。 现在,无论年份如何,此新代码都将起作用。 我更改了它,因为显然,生日不算年份。 如果某人的生日是“ 2015-04-10”之类的生日,即使相距仅2天零1天,该日期也将是最接近的,因为这仅是1天,这将很有用。

我在代码中写了一条注释。 这意味着如果要考虑年份,请设置$format = "Ymd"但是如果您不想考虑年份,请设置$format = "md" ,这将减去几个月又几天。

<?php
$format = "Y-m-d"; // change to "m-d" to ignore years
$today = date($format);

$Bdays = array
(
    array("Lucienne Adkisson", strtotime("2017-10-17")),
    array("Sheryll Nestle", strtotime("2017-02-16")),
    array("Tim Pittman", strtotime("2017-07-29")),
    array("Elmer Letts", strtotime("2017-12-01")),
    array("Gino Massengale", strtotime("2016-04-16")),
    array("Jeremy Wiggs", strtotime("2017-09-17")),
    array("Julian Bulloch", strtotime("2017-06 -21")),
    array("Joella Hinshaw", strtotime("2017-06-25")),
    array("Mamie Burchill", strtotime("2017-11-15")),
    array("Constance Segers", strtotime("2017-06-30")),
    array("Jessy Pinkmann", strtotime("2017-09-11")),
    array("Dudley Currie", strtotime("2017-02-10"))
);


$tmp = 366;
$closest = ["", 0];
for ($i = 0; $i < count($Bdays); $i++)
{
    if (abs($today - date($format, $Bdays[$i][1])) < $tmp)
    {
        $tmp = abs($today - date($format, $Bdays[$i][1]));
        $closest = [$Bdays[$i][0], date("Y-m-d", $Bdays[$i][1])];
    }
}
?>

尝试这个:

DateTime为您提供了一种diff方法,该方法易于使用。 如果天数的精度足够,则可以使用该结果的days属性来比较日期。 然后,您必须存储最低增量的索引,最后可以获取数组中的相应元素

<?php

$today = new DateTime();

$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"))
);

$len = count($Bdays);
$minDiff = PHP_INT_MAX;
$minIndex = -1;

for ($i = 0; $i < $len; $i++) {
    $diff = $today->diff($Bdays[$i][1]);

    if ($diff->days < $minDiff) {
        $minIndex = $i;
        $minDiff = $diff->days;
    }
}

print_r($Bdays[$minIndex]);

您可以尝试以下代码:

 <?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>'; ?> 

没事的。 这是输出:

在此处输入图片说明

这是“ Arrayy”类的示例。 在示例中,“ $ currentDate”是固定的,因此每天的结果都是相同的。

单元测试: https : //github.com/voku/Arrayy/blob/master/tests/ArrayyTest.php#L2511

安装: composer require voku/arrayy

例:

$birthDates = 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')),
);

$birthDatesAraayy = new Arrayy($birthDates);
$currentDate = new \DateTime('2017-09-11');
$format = 'Y-m-d H:i:s';

/**
 * sort by date - helper-function
 *
 * @param array $a
 * @param array $b
 *
 * @return int
 */
$closureSort = function ($a, $b) use ($format) {
  /* @var $aDate \DateTime */
  /* @var $bDate \DateTime */
  $aDate = $a[1];
  $bDate = $b[1];

  if ($aDate->format($format) === $bDate->format($format)) {
    return 0;
  }

  return $aDate->format($format) > $bDate->format($format) ? -1 : 1;
};

/**
 * reduce by date - helper-function
 *
 * @param array $resultArray
 * @param array $value
 *
 * @return array
 */
$closureReduce = function ($resultArray, $value) use ($currentDate) {
  /* @var $valueDate \DateTime */
  $valueDate = $value[1];

  $valueDateInterval = $currentDate->diff($valueDate);

  if ($valueDateInterval->format('%R%a') >= 0) {
    $resultArray['thisYear'][] = $value;
  } else {
    $value[1] = $valueDate->modify('+1 year');

    $resultArray['nextYear'][] = $value;
  }

  return $resultArray;
};

//
// reduce && sort the array
//

/* @var $resultMatch Arrayy|Arrayy[] */
$resultMatch = $birthDatesAraayy->reduce($closureReduce);

$thisYear = $resultMatch['thisYear']->customSortValues($closureSort);
$nextYear = $resultMatch['nextYear']->customSortValues($closureSort);

$resultMatch = $nextYear->reverse()->mergePrependNewIndex($thisYear->reverse()->getArray());

//
// check the result
//

self::assertEquals(
    array(
        array('Jessy Pinkmann', date_create('2017-09-11')),
        array('Jeremy Wiggs', date_create('2017-09-17')),
        array('Lucienne Adkisson', date_create('2017-10-17')),
        array('Mamie Burchill', date_create('2017-11-15')),
        array('Elmer Letts', date_create('2017-12-01')),
        array('Dudley Currie', date_create('2018-02-10')),
        array('Sheryll Nestle', date_create('2018-02-16')),
        array('Gino Massengale', date_create('2018-04-16')),
        array('Julian Bulloch', date_create('2018-06 -21')),
        array('Joella Hinshaw', date_create('2018-06-25')),
        array('Constance Segers', date_create('2018-06-30')),
        array('Tim Pittman', date_create('2018-07-29')),
    ),
    $resultMatch->getArray()
);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM