2013-02-15 186 views
0

如果通過或不通過,我需要檢查一個特定的日期,如果通過,它會根據日期數組進行檢查,以查看哪個日期最近。PHP:根據日期數組檢查日期以獲取最近的日期

我已經開始了,但

代碼:

<?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通過,它將對現有的一些未來的日期檢查,對於相同的課程,在特定的數據庫表中的某處。 當檢查針對數組$ all_course_dates_arr [] $ course_nxt_date,我將需要獲得最近的日期到$ course_nxt_date

Example of dates that could be in the array - $all_course_dates_arr[]: 

     $all_course_dates_arr[0] = "2013-01-25"; 
     $all_course_dates_arr[1] = "2013-04-08"; 
     $all_course_dates_arr[2] = "2013-06-13"; 
     $all_course_dates_arr[3] = "2013-08-03"; 
     $all_course_dates_arr[4] = "2013-02-17"; 

由於

$course_nxt_date = "2013-02-03"; 

該函數應該輸出如下所示的最近的日期:

echo getCurrDate(18); 

Output - 2013-02-17 

我很樂意爲您解答這個問題......謝謝!

+1

如果您使用[時代時間戳](http://en.wikipedia.org/wiki/Epoch),您可能會比較容易地比較這些值。你可以使用類似於[this](http://stackoverflow.com/a/6147488/558021)的東西來獲得最接近的匹配。 – Lix 2013-02-15 14:20:02

+0

我認爲在查詢中使用'DATEDIFF'將會更容易從數據庫中檢索最近的日期。 – hsan 2013-02-15 14:22:54

+0

使用日期庫的時間戳可以讓您找到兩天之間的差異。爲此,您將不得不正確使用自定義功能 – 2013-02-15 14:23:15

回答

0

可以使用的strtotime得到一個時間戳,然後通過數組循環,同時保持最小的差的軌跡:

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

類似的東西 - 還沒來得及對它進行測試。只是在這裏輸入它,但我相信這個邏輯是正確的。

2

你會更好,在DB這樣做:

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

我會做檢查的SQL,如下圖所示。只要確保你的sql在這樣做的時候是安全的。

​​

因此,這將返回一個結果,與最接近於給定日期之日起下一個療程。

希望這會有所幫助,祝你好運:)

0

如果使用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個變量並獲得差異,並且在一個循環中可以檢查哪個具有最短的差異。