Get the last Thursday, but only if it's the second Thursday in this sequence

I know I can get last Thursday with this

$firstdate = strtotime('last thursday'); 

However, from the moment this question is published, it will return

 August 2nd, 2012 

I need the last Thursday following this two-week cycle:

  • August 9th
  • August 23
  • 6 September
  • September 20

And every second Thursday after that.

  • So, if the current date is August 10th, she will be back on August 9th

  • If the current date is August 21, it will also return on August 9 (even if the previous Thursday is August 16).

  • If the current date is August 24th, she will return on August 23rd

  • If the current date is September 1, it will also return on August 23 (although the previous Thursday is August 30)

+4
source share
1 answer

Use the week number.

What you want is Thursdays of weeks that have an even number .

Here is one way to do this:

 function getLastEvenWeekThursday() { $lastThursday = strtotime('last thursday'); $weeknumber = date("W", $lastThursday); if($weeknumber % 2 == 0) return $lastThursday; else return $lastThursday - 3600*24*7; } 
+4
source

All Articles