if (first wednesday and first thursday of each month) else

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gstricklind
Forum Newbie
Posts: 1
Joined: Fri Feb 03, 2012 4:18 pm

if (first wednesday and first thursday of each month) else

Post by gstricklind »

I've been having some trouble with a script that is suppose to seemly to do something simple but its actually been quite difficult. Let me try to explain:

The FIRST FRIDAY of each month, lets say a meeting is held.
This group has online registration for this meeting. The WEDNESDAY & THURSDAY prior to the first Friday, registration for the meeting must be closed. After those wed and thurs, they would like to accept registration again (yes, beginning the first friday).
But, what if The wednesday before the first Friday is still the previous month? What if the Wednesday AND Thursday before the first Friday are in the previous month?

Hopefully that's understandable?

My first attempt was:

Code: Select all

<?php
$firstwed = strtotime("first Wednesday, first Thursday". date("F Y"));
$now = strtotime('today'); 

if( $firstwed == $now) {
  echo "Registration is closed until our next event!";
  // do something
} else {
  echo 'REGISTER AND PAY ONLINE HERE';
  // do something else
}
?>
Obviously that didn't work. Someone suggested this:

Code: Select all

// get date object for today
$today = new DateTime();

// get the date object for the first friday of the current month
$ffstr = 'First Friday of ' . date('F') .' '. date('Y');
$first_friday = new DateTime($ffstr);

// create a cutoff date object two days before the first friday
$cutoff_date = clone $first_friday;
$cutoff_date->sub(new DateInterval('P2D'));

// compare today's date to the cutoff date/first friday date
if ($today < $cutoff_date || $today > $first_friday) {
  echo 'REGISTER AND PAY ONLINE HERE';
} else {
  echo "Registration is closed until our next event!";
}  
But that basically broke my page and I learned that DateInterval will only work on PHP 5.3.1+ systems.
So they came up with this:

Code: Select all

ff = strtotime('first friday ' . date('F Y'));
$twoDaysPast = strtotime('-2 day', $ff);
$oneDaysPast = strtotime('-1 day', $ff);  
But I'm still a noob :\ I can read and understand it pretty well but I have no idea how to implement this? Can anyone help? And thank you!
Post Reply