1

I need to check a specific date if it's passed or not and if it's passed, it will then be checked against an array of date to see which is closest.

I've started out but

The code:

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

    }

?>

Further detail:

if the $course_nxt_date is passed, it will be checked against some existing future dates, for the same course, somewhere in a particular database table. While checking $course_nxt_date against the array $all_course_dates_arr[], i will need to get the nearest date to the $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"; 

Since

$course_nxt_date = "2013-02-03";

The function should output the nearest date as shown below:

echo getCurrDate(18);

Output - 2013-02-17

I'll be glad to get help with this...Thanks!

Sms
  • 147
  • 1
  • 9
  • 20
  • 1
    If you use [epoch timestamps](http://en.wikipedia.org/wiki/Epoch), it might be easier for you to compare the values. You could use something similar to [this](http://stackoverflow.com/a/6147488/558021) to get the closest match. – Lix Feb 15 '13 at 14:20
  • I think it would be easier to use `DATEDIFF` in your query to only retrieve the closest date from the database. – hsan Feb 15 '13 at 14:22
  • Using timestamp of date library would allow you to find difference between two days. For this you will have to right a custom function – Jehanzeb.Malik Feb 15 '13 at 14:23

4 Answers4

2

You can use strtotime to get a timestamp, then loop through the array, while keeping track of the smallest difference:

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

Something like that - haven't had time to test it. Just typed it up here, but I believe the logic is sound.

Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70
Julio
  • 2,261
  • 4
  • 30
  • 56
  • This really helped me get the latest date in an array so that I could again compare it to the current date. Helped so much! – Robert Dewitt Mar 13 '19 at 17:03
2

You'd be better off doing it in the DB:

SELECT DATEDIFF(curdate(), course_date) AS diff
...
WHERE course_date >= curdate()
ORDER BY diff ASC
LIMIT 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
2

If using php5.3+ this is possibly the easiest way to do this.

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

As you are not including the time, the shortest timespan you can get to match is days. So now you can send 2 variables and get the difference and in a loop can check which has the shortest difference.

Jehanzeb.Malik
  • 3,332
  • 4
  • 25
  • 41
1

I would do the check in the sql, like below. Just make sure your sql is secure when doing so.

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

So this will return one result, with the next course with the date closest to the given date.

Hope this helps, good luck :)

Wessel
  • 81
  • 4