1

特定の日付が渡されたかどうかを確認する必要があり、渡された場合は、日付の配列に対してチェックされ、どれが最も近いかが確認されます。

始めましたが、

コード:

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

喜んでお手伝いさせていただきます...ありがとうございます!

4

4 に答える 4

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 つの変数を送信して差を取得し、ループ内で差が最も短い変数を確認できます。

于 2013-02-15T14:31:21.423 に答える
2

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;

そのようなもの - テストする時間がありませんでした。ここに入力しただけですが、論理は正しいと思います。

于 2013-02-15T14:24:05.923 に答える
2

DBでそれを行うほうがよいでしょう:

SELECT DATEDIFF(curdate(), course_date) AS diff
...
WHERE course_date >= curdate()
ORDER BY diff ASC
LIMIT 1
于 2013-02-15T14:26:11.383 に答える
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");

したがって、これは 1 つの結果を返し、次のコースは指定された日付に最も近い日付になります。

これが役に立てば幸いです、頑張ってください:)

于 2013-02-15T14:26:40.217 に答える