I am using WordPress as my CMS.
I am trying to check if some of my user has a birthday in current week.
with no success.
Here is my code
$fd = date("Y-m-d",strtotime('monday this week'));
$ld = date("Y-m-d",strtotime("sunday this week"));
$cdate = date('m-d',time());
if (($cdate >= $fd) && ($cdate <= $ld)) {
echo 'true';
} else {
echo 'false';
}
This is returning false for me
If i use
'm-d' in $cdate variable
It works fine if is use Y-m-d
but in that case , the years should be same which isnt possible as all the people have different birth years
Here’s My Way
To find it you can do like this
Step 1 :
Find the Start and Last Day of the Week
Step 2 :
See if the given date is between the Start and Last Day of the Week
So, Finally the Code you shall have is
Note
You can have your own Start Day i.e.,
Sunday
orMonday
You can have your own Date or Current Date
You can use the format parameter
W
, which will give you the number of the current weak in the year (the calender week).$birthday
have to be a timestamp here. Maybe you have to use$birthday = strtotime($birthdate);
.Richard’s answer is spot on. Just for people who reach this post looking for how to check the week is the same and also check that the year is the same; this will also check they are not only in the same week of the year, but the same year also.
In my opinion, the easiest solution is to pass strings to date and keep them as a number, as you can see in this example:
Of course, you’ll have to change the birthday year to the current one.