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?
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.
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)); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With