用å称显示最近的日期

时间:2017-10-02 08:53:54

标签: php

在this帖å­ä¸­ï¼Œæœ‰ä¸€ä¸ªç­”案显示以下代ç ï¼š

<?php
$dates = array
(
    '0' => "2013-02-18 05:14:54",
    '1' => "2013-02-12 01:44:03",
    '2' => "2013-02-05 16:25:07",
    '3' => "2013-01-29 02:00:15",
    '4' => "2013-01-27 18:33:45"
);

function closest($dates, $findate)
{
    $newDates = array();

    foreach($dates as $date)
    {
        $newDates[] = strtotime($date);
    }

    echo "<pre>";
    print_r($newDates);
    echo "</pre>";

    sort($newDates);
    foreach ($newDates as $a)
    {
        if ($a >= strtotime($findate))
            return $a;
    }
    return end($newDates);
}

$values = closest($dates, date('2013-02-04 14:11:16'));
echo date('Y-m-d h:i:s', $values);

如何显示日期,而ä¸æ˜¯æ˜¾ç¤ºæ—¥æœŸï¼Ÿåœ¨è¿™ç§æƒ…况下:2。

感谢您的帮助。

4 个答案:

答案 0 :(得分:1)

$dates = array ( "2017-10-25 01:44:03", "2017-10-20 05:14:54",
"2017-12-05 16:25:07",  "2017-11-15 02:00:15");

function find_closest( $array, $date ) {
    foreach($array as $day)
    {
        $interval[] = abs(strtotime($date) - strtotime($day));
    }

    asort($interval);

    $index = array_keys($interval, min($interval)); 

    $closed_bday_index = $index[0];

    echo "Person".$closed_bday_index." b'date ".$array[ $closed_bday_index ];
 }

find_closest($dates, "2013-02-18 05:14:55");

答案 1 :(得分:1)

计算给定日期值的最近key

$arr =['0' => "2013-02-18 05:14:54",
'1' => "2013-02-12 01:44:03",
'2' => "2013-02-05 16:25:07",
'3' => "2013-01-29 02:00:15",
'4' => "2013-01-27 18:33:45"];
$date = "2013-02-04 14:11:16";
$near = "";
$lowest = 0;
foreach($arr as $key=>$value){
  $diff = abs(strtotime($date)-strtotime($value));
  if(!$lowest || $diff < $lowest) {
    $near = $key;
    $lowest = $diff;
  }
}
print_r("Nearest key is : $near");

现场演示:https://eval.in/872057

输出将为

2013-02-04 =&gt; 2 //其近期2013-02-05

2013-02-06 =&gt; 2 //å†æ¬¡æŽ¥è¿‘2013-02-05

答案 2 :(得分:0)

$dates = array
(
    '0' => "2013-02-18 05:14:54",
    '1' => "2013-02-12 01:44:03",
    '2' => "2013-02-05 16:25:07",
    '3' => "2013-01-29 02:00:15",
    '4' => "2013-01-27 18:33:45"
);


function find_closest($array, $date)
{
    //$count = 0;
    foreach($array as $day)
    {
        //$interval[$count] = abs(strtotime($date) - strtotime($day));
        $interval[] = abs(strtotime($date) - strtotime($day));
        //$count++;
    }

    asort($interval);
    $closest = key($interval);

    echo $array[$closest];
}

find_closest($dates, "2013-02-04 14:11:16");

答案 3 :(得分:0)

ä½ å¿…é¡»åªè¿”回数组的密钥。

$dates = array
(
    '0' => "2013-02-18 05:14:54",
    '1' => "2013-02-12 01:44:03",
    '2' => "2013-02-05 16:25:07",
    '3' => "2013-01-29 02:00:15",
    '4' => "2013-01-27 18:33:45"
);

function closest($dates, $findate)
{
    $newDates = array();

    foreach($dates as $date)
    {
        $newDates[] = strtotime($date);
    }

    echo "<pre>";
    print_r($newDates);
    echo "</pre>";

    sort($newDates);
    foreach ($newDates as $key=>$a)
    {
        if ($a >= strtotime($findate))
            return $key;
    }
    return $key;
}

$values = closest($dates, date('2013-02-04 14:11:16'));
echo  $values;

<强>输出

Array
(
    [0] => 1361182494
    [1] => 1360651443
    [2] => 1360099507
    [3] => 1359442815
    [4] => 1359329625
)
2