View Full Version : first wednesdays and first thursday of the month

01-19-2012, 08:53 PM
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)

Hopefully that's understandable? So I came up with this:

<!-- determines first Wednesday and first Thursday of the month to echo something different than the rest of the days of the month. -->
$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 {
// do something else

But then soon realized, that didn't work. There's a few problems with the "first Thursday", right? Plus, the fact that those Wednesdays and Thursdays wont trigger like I would need if the first Friday ends up being, lets say, the 2nd.

Second attempt, but this broke my page and half of the content went blank:

// 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) {
} else {
echo "Registration is closed until our next event!";

Should I be going about this a different way? I thought is was going to be simple, but now it's getting to be a headache.

01-19-2012, 10:36 PM
First Friday OF a month will not work. OF is not a valid specifier.
What will work is 'First friday January 2012' for example.
DateInterval will only work on PHP 5.3.1+ systems. There is also no need to really use it for this task.

$ff = strtotime('first friday ' . date('F Y'));
$twoDaysPast = strtotime('-2 day', $ff);
$oneDaysPast = strtotime('-1 day', $ff);

Simple as that.

01-19-2012, 11:12 PM
Ok great. Thanks! I'm sorry, I'm still trying to get a handle on this PHP stuff.. what would the rest of the code look like now? Thanks for your help!

01-19-2012, 11:24 PM
First Friday OF a month will not work. OF is not a valid specifier.
What will work is 'First friday January 2012' for example.

It should: http://uk.php.net/manual/en/datetime.formats.relative.php

'first day' ' of'? Sets the day of the first of the current month. This phrase is best used together with a month name following it. "first day of January 2008"

Oddly though I was using this myself locally in a project with php 5.3.0 and when I uploaded to a server with php 5.2.7 it refused to work causing some minor annoyance. There is no mention of this in the php manual and the manual is normally very good at mentioning differences between versions.

01-20-2012, 04:53 PM
I thought it did work as well, but when I hit up codepad it always returned 0. Couldn't find the docs for this in php, so I went straight for the gnu and it didn't have 'of' as a valid modifier in there (that I could find).
Using just 'first x' was always working relative to week, so 'first friday' run today returns January 27th.

So yes, 'OF' will definitely work, but not at codepad. Not sure what they've done differently; OF was valid in 5.2.17 (the only 5.2x version I have still), but they're version is 5.2.5.

01-20-2012, 08:10 PM
Yes I noticed it wouldn't work on codepad too then I tried my main website and it didn't work there which was also on windows like my localhost and that was how i realised it was a version problem.

Interesting that it works without 'of' though, that will make life a little easier :thumbsup: