我需要检查一个特定日期,如果它已通过,如果它已通过,则会根据日期数组检查它,看哪个最接近。
我刚开始,但是
代码:
<?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
我很乐意得到这方面的帮助......谢谢!
答案 0 :(得分: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;
这样的事情 - 没有时间去测试它。只需在此输入,但我相信逻辑是合理的。
答案 1 :(得分:2)
你最好在数据库中做这件事:
SELECT DATEDIFF(curdate(), course_date) AS diff
...
WHERE course_date >= curdate()
ORDER BY diff ASC
LIMIT 1
答案 2 :(得分: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");
因此,这将返回一个结果,下一个课程的日期最接近给定日期。
希望这会有所帮助,祝你好运:)
答案 3 :(得分:1)
如果使用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个变量并获得差异,并在一个循环中可以检查哪个变量最短。