Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:根据日期数组检查日期以获得最近的日期_Php_Arrays_Date - Fatal编程技术网

PHP:根据日期数组检查日期以获得最近的日期

PHP:根据日期数组检查日期以获得最近的日期,php,arrays,date,Php,Arrays,Date,我需要检查一个特定的日期是否已通过,如果已通过,则将根据日期数组进行检查,以查看哪个日期最近 我已经开始了,但是 守则: <?php function getCurrDate ($c_id){ // Fetch the course date $course_nxt_date = "2013-02-03"; // fetch current date $today = date("Y-m-d"); // Check if course date is i

我需要检查一个特定的日期是否已通过,如果已通过,则将根据日期数组进行检查,以查看哪个日期最近

我已经开始了,但是

守则:

<?php

    function getCurrDate ($c_id){   

// Fetch the course date    
$course_nxt_date = "2013-02-03";

// fetch current date
   $today = date("Y-m-d");

// Check if course date is in the future
if($course_nxt_date > $today){
    $course_date = $course_nxt_date;
    return $course_date;
}
// Check if course date is exactly today
elseif($course_nxt_date == $today){
    $course_date = $course_nxt_date;
    return $course_date;    
}
// Check if course date is passed
else{   

// Since course date is passed, get an array of future dates from database
$all_course_dates_query = @mysql_query("select * from pub_calendar_dates where course_id = '$c_id' order by course_date asc");

//Loop through the array
        $all_course_dates_arr = array();
        while ($all_course_dates_row = @mysql_fetch_assoc($all_course_dates_query)){
// assign each variable in the $all_course_dates_row to a new array $all_course_dates_arr
                  $all_course_dates_arr[] = $all_course_dates_row['course_date'];
        }

// This is where I became blank on what to do next and Im stucked...Need help from here

        return $course_date;        
}   

    }

?>

$course_nxt_date = "2013-02-03";
函数应输出最接近的日期,如下所示:

echo getCurrDate(18);

Output - 2013-02-17

我很高兴能在这方面得到帮助…谢谢

您可以使用strotime获取时间戳,然后在数组中循环,同时跟踪最小的差异:

$date_check = strtotime("02-15-2013"); // Gives you a timestamp of the date

$difference       = NULL; // Holds the difference of the closest date
$difference_index = NULL; // Holds the index in the array

for($i = 0; $i < count($dates_arr); $i++) {
    $d = $dates_arr[$i]; // May need to convert $d into a timestamp if it isn't already

    $diff = abs($d - $date_check); // abs to get the absolute difference

    // If the difference is smaller than the absolute difference of the last date
    // we need to update our values here
    if($difference == NULL || $diff < $difference) {
        $difference = $diff;
        $difference_index = $i;
    }
}

print "The closest should be at index " . $difference_index;
$date\u check=strotime(“02-15-2013”);//给你一个日期的时间戳
$difference=NULL;//保存最近日期的差值
$difference\u index=NULL;//保存数组中的索引
对于($i=0;$i

像这样的东西-还没有时间测试它。在这里输入,但我相信逻辑是正确的。

您可以使用strotime获取时间戳,然后在数组中循环,同时跟踪最小的差异:

$date_check = strtotime("02-15-2013"); // Gives you a timestamp of the date

$difference       = NULL; // Holds the difference of the closest date
$difference_index = NULL; // Holds the index in the array

for($i = 0; $i < count($dates_arr); $i++) {
    $d = $dates_arr[$i]; // May need to convert $d into a timestamp if it isn't already

    $diff = abs($d - $date_check); // abs to get the absolute difference

    // If the difference is smaller than the absolute difference of the last date
    // we need to update our values here
    if($difference == NULL || $diff < $difference) {
        $difference = $diff;
        $difference_index = $i;
    }
}

print "The closest should be at index " . $difference_index;
$date\u check=strotime(“02-15-2013”);//给你一个日期的时间戳
$difference=NULL;//保存最近日期的差值
$difference\u index=NULL;//保存数组中的索引
对于($i=0;$i

像这样的东西-还没有时间测试它。只是在这里输入,但我相信逻辑是正确的。

您最好在数据库中执行此操作:

SELECT DATEDIFF(curdate(), course_date) AS diff
...
WHERE course_date >= curdate()
ORDER BY diff ASC
LIMIT 1

您最好在DB中执行此操作:

SELECT DATEDIFF(curdate(), course_date) AS diff
...
WHERE course_date >= curdate()
ORDER BY diff ASC
LIMIT 1

我会在sql中进行检查,如下所示。这样做时,只需确保sql是安全的

$result = @mysql_query("select TOP 1 * from pub_calendar_dates where course_id = '$c_id' AND course_date >= '$course_nxt_date' order by course_date asc");
因此,这将返回一个结果,下一个课程的日期最接近给定日期


希望这有帮助,祝你好运:)

我会在sql中进行检查,如下所示。这样做时,只需确保sql是安全的

$result = @mysql_query("select TOP 1 * from pub_calendar_dates where course_id = '$c_id' AND course_date >= '$course_nxt_date' order by course_date asc");
因此,这将返回一个结果,下一个课程的日期最接近给定日期


希望这有帮助,祝你好运:)

如果使用php5.3+这可能是最简单的方法

$days = getDifference("2013-02-03","2013-01-25");

function getDifference($date1, $date2){

    // Format for date variables is "YYYY-MM-DD"
    $objDate1 = new DateTime($date1);
    $objDate2 = new DateTime($date2);
    $interval = $objDate1->diff($objDate2);

    return $interval->days; //This would return the difference in number of days
}

由于您不包括时间,因此可以匹配的最短时间间隔为天。因此,现在您可以发送2个变量并获得差异,在循环中可以检查哪一个差异最短。

如果使用php5.3+这可能是最简单的方法

$days = getDifference("2013-02-03","2013-01-25");

function getDifference($date1, $date2){

    // Format for date variables is "YYYY-MM-DD"
    $objDate1 = new DateTime($date1);
    $objDate2 = new DateTime($date2);
    $interval = $objDate1->diff($objDate2);

    return $interval->days; //This would return the difference in number of days
}

由于您不包括时间,因此可以匹配的最短时间间隔为天。因此,现在您可以发送2个变量并获取差值,在循环中可以检查哪一个变量的差值最短。

如果使用,比较这些值可能会更容易。您可以使用类似的方法来获取最接近的匹配。我认为在查询中使用
DATEDIFF
更容易从数据库中检索最接近的日期。使用日期库的时间戳可以让您找到两天之间的差异。为此,您必须为自定义函数设置权限。如果使用,比较值可能会更容易。您可以使用类似的方法来获取最接近的匹配。我认为在查询中使用
DATEDIFF
更容易从数据库中检索最接近的日期。使用日期库的时间戳可以让您找到两天之间的差异。为此,您必须设置一个自定义函数这确实帮助我获得了数组中的最新日期,以便我可以再次将其与当前日期进行比较。帮了这么多忙!这确实帮助我获得了数组中的最新日期,这样我就可以再次将其与当前日期进行比较。帮了这么多忙!