I have created a function that will determine if an “offer” is currently valid. Each offer has a start and end date stored in a database and I use the date() function to figure if its valid. However, I have a problem since the following scenario is not returning “TRUE”. I believe it should return “TRUE”. What am I not getting?
$start = 2010-9-18
$stop = 2010-10-10
// Current date is 2010-9-19
function is_active($start, $stop) {
$now = date("Y-n-d");
if($now >= $start && $now <= $stop) {
return true;
}
}
?>
Thanks for helping out!
date()returns a string. Strings are compared by lexicographical ordering. In this ordering,9is larger than10because1<anything>appears before9<anything>.One simple fix is to use
Y-m-dinstead ofY-n-d, and record the$startas'2010-09-18'.