PHP check if the dates are in current week

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

Read More
$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

Related posts

4 comments

  1. Here’s My Way

    To find it you can do like this

    Step 1 :

    Find the Start and Last Day of the Week

    $FirstDay = date("Y-m-d", strtotime('sunday last week'));  
    $LastDay = date("Y-m-d", strtotime('sunday this week'));  
    

    Step 2 :

    See if the given date is between the Start and Last Day of the Week

      if($Date > $FirstDay && $Date < $LastDay) {
           echo "It is Between";
        } else {
            echo "No Not !!!";  
        } 
    

    If Yes then It belongs Else Not

    So, Finally the Code you shall have is

    <?php
    $Date = "2015-06-01"; #Your Own Date
    $Date = date('Y-m-d'); #Or Current Date Fixed here
    $FirstDay = date("Y-m-d", strtotime('sunday last week'));  
    $LastDay = date("Y-m-d", strtotime('sunday this week'));  
    if($Date > $FirstDay && $Date < $LastDay) {
       echo "It is Between";
    } else {
        echo "No Not !!!";  
    }  
    ?>
    

    Note

    1. You can have your own Start Day i.e., Sunday or Monday

    2. You can have your own Date or Current Date

  2. You can use the format parameter W, which will give you the number of the current weak in the year (the calender week).

    if(date("W") == date("W", $birthday)){
        // User has birthday this week
    }
    

    $birthdayhave to be a timestamp here. Maybe you have to use $birthday = strtotime($birthdate);.

  3. 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.

    $testData = "1447672336";
    if((date("W") == date("W", $testData)) && (date("Y") == date("Y", $testData))){
        // The timestamp in $testData is the same week of the same year as today is.
    }
    
  4. 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:

    $fd = strtotime('monday this week'); // First date
    $ld = strtotime('sunday this week'); // last date
    
    $birthday_date = strtotime('YYYY-mm-dd'); // Birthday date
    
    if (($birthday_date > $fd) && ($birthday_date < $ld)) {
      echo 'true';
    } else {
      echo 'false';
    }
    

    Of course, you’ll have to change the birthday year to the current one.

Comments are closed.