Reputation: 15
I would like to test to see if the date today is within the range of rangeBegin
and rangeEnd
.
Currently the code below returns false. Using echo $today
results in the value being 17/12 which is correct for the value of today
<?php
$today = date('d.m');
$today= date('d/m', strtotime($today));;
$rangeBegin = strtotime("01-12");
$rangeEnd = strtotime("02-01");
if($rangeBegin <= $today && $rangeEnd >= $today)
{
//in range
}
else
{
//not in range
}
?>
Please could someone advise me where I have gone wrong in the above code in order to test to see if the date today is between the two ranges.
I.E. as it is the 17th Dec, it should return true as it is between 1st Dec and 2nd Jan.
Upvotes: 1
Views: 56
Reputation: 10033
Remember that 2 january is next year. And strtotime doesn't work like that.
$today = new DateTime();
$year = (int) $today->format("Y");
$rangeBegin = DateTime::createFromFormat("Y-m-d H:i:s", "$year-12-01 00:00:00");
$rangeEnd = clone $rangeBegin;
$rangeEnd = $rangeEnd->modify("+32 days");
if ($rangeBegin <= $today && $rangeEnd >= $today) {
echo "true";
} else {
echo "false";
}
Set the correct time zone somewhere.
date_default_timezone_set("Europe/Oslo");
Upvotes: 3