如果通过或不通过,我需要检查一个特定的日期,如果通过,它会根据日期数组进行检查,以查看哪个日期最近。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
我很乐意为您解答这个问题......谢谢!
如果您使用[时代时间戳](http://en.wikipedia.org/wiki/Epoch),您可能会比较容易地比较这些值。你可以使用类似于[this](http://stackoverflow.com/a/6147488/558021)的东西来获得最接近的匹配。 – Lix 2013-02-15 14:20:02
我认为在查询中使用'DATEDIFF'将会更容易从数据库中检索最近的日期。 – hsan 2013-02-15 14:22:54
使用日期库的时间戳可以让您找到两天之间的差异。为此,您将不得不正确使用自定义功能 – 2013-02-15 14:23:15