Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a date is in a given range?

If you have a $start_date and $end_date, how can you check if a date given by the user falls within that range?

e.g.

$start_date = '2009-06-17';  $end_date = '2009-09-05';  $date_from_user = '2009-08-28';  

At the moment the dates are strings, would it help to convert them to timestamp integers?

like image 961
meleyal Avatar asked Jun 10 '09 16:06

meleyal


People also ask

How do you check if a date is in a given range in Java?

A Java example to check if a provided date is within a range of 3 momths before and after current date. The idea is quite simple, just use Calendar class to roll the month back and forward to create a “date range”, and use the Date. before() and Date. after() to check if the Date is within the range.


1 Answers

Converting them to timestamps is the way to go alright, using strtotime, e.g.

$start_date = '2009-06-17';  $end_date = '2009-09-05';  $date_from_user = '2009-08-28';  check_in_range($start_date, $end_date, $date_from_user);   function check_in_range($start_date, $end_date, $date_from_user) {   // Convert to timestamp   $start_ts = strtotime($start_date);   $end_ts = strtotime($end_date);   $user_ts = strtotime($date_from_user);    // Check that user date is between start & end   return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); } 
like image 138
ConroyP Avatar answered Sep 23 '22 07:09

ConroyP