Php 检查日期数组中的日期并对其进行排列

Php 检查日期数组中的日期并对其进行排列,php,Php,我有这样一个数组 $dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20'); $dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20'); echo "<pre>"; print_r($dates); $dateArray=array(); foreach($dates as $row) $dateArray[] = da

我有这样一个数组

$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');

echo "<pre>";
print_r($dates);
$dateArray=array();

foreach($dates as $row)
$dateArray[] = date('Y-m-d', strtotime($row));

$current_date = date('Y-m-d', strtotime('2012-10-11'));
array_push($dateArray,$current_date);

sort($dateArray);

echo "<br />";
print_r($dateArray);
echo "</pre>";

echo "<br />";

$cd_index= array_search($current_date, $dateArray);

if($cd_index>0)
{
    $pastdate=$dateArray[$cd_index-1];
    echo "Pastarray-".$pastdate;
}
else
echo "No Past Date";
现在如果我有一个最新的约会

$current_date = '2010-10-11';
我怎样才能找到离这最近的过去日期呢。在这种情况下是2012年10月7日

谢谢

像这样试试

$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');

echo "<pre>";
print_r($dates);
$dateArray=array();

foreach($dates as $row)
$dateArray[] = date('Y-m-d', strtotime($row));

$current_date = date('Y-m-d', strtotime('2012-10-11'));
array_push($dateArray,$current_date);

sort($dateArray);

echo "<br />";
print_r($dateArray);
echo "</pre>";

echo "<br />";

$cd_index= array_search($current_date, $dateArray);

if($cd_index>0)
{
    $pastdate=$dateArray[$cd_index-1];
    echo "Pastarray-".$pastdate;
}
else
echo "No Past Date";
$dates=array('2012-10-02'、'2012-10-03'、'2012-10-07'、'2012-10-20');
回声“;
打印(日期);
$dateArray=array();
foreach($行中的日期)
$dateArray[]=date('Y-m-d',strottime($row));
$current_date=日期('Y-m-d',标准时间('2012-10-11');
数组\u推送($dateArray,$current\u date);
排序($dateArray);
回声“
”; 打印(日期数组); 回声“; 回声“
”; $cd\u index=array\u search($current\u date,$dateArray); 如果($cd_索引>0) { $pastdate=$dateArray[$cd_index-1]; 回显“Pastarray-”$pastdate; } 其他的 呼应“没有过去的日期”;
未来日期:

<?php
$dates = array('2012-10-07','2012-10-02','2012-10-03', '2012,10,20');
$current_date = date('2010-10-11');
$closest='';
foreach($dates as $d)
{
if( date('y-m-d',strtotime($d))<$current_date && $d>$closest)
{
$closest=$d;

}

}
echo $closest;

if($cd_index这也可以使用

// your test data
$dates = array('2012-10-02','2012-10-03','2012-10-07', '2012-10-20');
$current_date = '2012-10-11';

// array to help analyzing the file
$myResolver = array();

// processing the array and calculating the unixtimestamp-differences
array_walk($dates,'calc_ts', array(&$myResolver, $current_date));

// sorting the array
asort($myResolver);

// fetching the first (aka smallest) value
$closest = key($myResolver);

var_dump($closest);

// calculating the unixtimestam-differences    
function calc_ts($item, $index, $d) {
    $d[0][$item] = abs(strtotime($item) - strtotime($d[1]));
}
我的解决方案:


适用于所需日期之前和之后的日期。

您最后一个值在
$dates
数组中的格式是否是预期的?为什么距离
2010-10-11
最近的日期是
2012-10-07
?它不是
2012-10-02
?这不是只有在2010-10-11实际在数组中时才有效吗?我已经查过了修改代码,谢谢如果我想查看最近的未来日期怎么办?我会在这里更改什么部分?谢谢我在相同的答案中更新了。。