Jump to content

Find every first and third Thursdays in every month, and display the next one.


DaveLinger

Recommended Posts

I'm designing a website for a car club, and on their info page it says that they meet on every first and third Thursdays of each month except December. I think it would be a cool added trick to just have a line under that that would say "The next meeting is on July 5", or whatever. How could I accomplish this?

Link to comment
Share on other sites

The very easiest approach would be to create a table for "events" (Or something). And simple add these to the table and store the event date as a timestamp (use columns like event id, event description, event information, event date...etc blah blah) and grab them for display purposes. You can then easily check with is the next even. That would be the easiest. :-)

Link to comment
Share on other sites

You can get away with using the strtotime() and date() functions:

 

<?php
$timestamp = strtotime('next thursday');
$date = date('F d',$timestamp);
echo 'The next meeting is on '.$date;
//produces The next meeting is on July 12
?>

 

You'll need to add in something to miss out any dates in december though.

 

EDIT: Whoops - i just realised i really didn't think this through, it wont work properly. Tis too late for me :P

Link to comment
Share on other sites

Example from php.net which could be useful:

<?php
$lastthurs=strtotime("last Thursday",mktime(0,0,0,date("n")+1,1));
?>

Returns the last thursday of this month.

<?php
$firstthurs=strtotime("next Thursday",mktime(0,0,0,date("n"),1));
?>

Returns the first thursday of this month.

 

So if I didn't need more coffee to stay awake, I'd work out the code for you.

1. Test whether you're past the first thursday of the month

2. If yes, get the date of the last thursday. If no, get the date of the first.

3. Convert it to a nice format

4. Echo it

Link to comment
Share on other sites

This was annoying me so i decided to sort it out. Firstly though, there is actualy a problem with the code for the first thursday. If the first thursday happened to be the first day of the month, currently we would get the 2nd thursday, as the function passes in the first day of the current month and then asks for NEXT thursday. So, in the mktime function, we actually need to pass in last month, and the last day of the last month:

$firstthurs=strtotime("next thursday",mktime(0,0,0,date("n")-1,date('t',mktime(0,0,0,date('n')-1))));

 

Anyways, heres what i came up with. Im pretty sure it works:

 

<?php
$curr_month = date('m');
$curr_day = date('d');
$firstthurs=strtotime("next thursday",mktime(0,0,0,date("n")-1,date('t',mktime(0,0,0,date('n')-1))));
$thirdthurs_day_no=date('d',$firstthurs+60*60*24*14);//need third thursday rather than last, so add two weeks

$firstthurs_day_no = date('d',$firstthurs);//get day number
if($curr_month >= 11 && $curr_day > $thirdthurs_day_no){//check to see if we are past the last meeting of the year - the 3rd thursday of november
$next_meeting = strtotime('next thursday',mktime(0,0,0,12,31,date('Y')));
$next_meeting = date('d F Y',$next_meeting);
}else{
if($curr_day < $firstthurs_day_no){//before first thursday of the month
	$next_meeting = date('d F Y',$firstthurs);
}elseif($curr_day < $thirdthurs_day_no){//between 1st and 3rd thursday
	$next_meeting = date('d F Y',$firstthurs+60*60*24*14);
}else{//after 3rd thursday of current month, so next meeting is 1st thursday of next month
	$next_meeting =strtotime("next Thursday",mktime(0,0,0,date("n"),date('t')));
	$next_meeting  =date('d F Y',$next_meeting);
}
}
echo 'The next meeting is on '.$next_meeting;
//produces The next meeting is on 19 July 2007
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.