Calendar - Returning date of the 2nd tuesday of month

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
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Calendar - Returning date of the 2nd tuesday of month

Post by tecktalkcm0391 »

I've been trying to get the date of the second tuesday of the month, but have had no luck... this is what I have so far...

Code: Select all

function get_firstday($month,$year){
            $num = date("w",mktime(0,0,0,$month,1,$year));
            if($num==1)
            return date("d",mktime(0,0,0,$month,1,$year));
            elseif($num>1)
            return date("d",mktime(0,0,0,$month,1,$year)+(86400*(8-$num)));
            else
            return date("d",mktime(0,0,0,$month,1,$year)+(86400*(1-$num)));
        }
It just isn't working... and I can't figure it out...

THANKS!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Calendar - Returning date of the 2nd tuesday of month

Post by VladSun »

Code: Select all

function get_second_tuesday($month,$year)
{
    return date("Y-m-d", strtotime("second tuesday", mktime(0, 0, 0, $month, 0, $year)));
}
 
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calendar - Returning date of the 2nd tuesday of month

Post by califdon »

I can't figure out what you had in mind as an algorithm. In the past, I've used this generic algorithm as an include file, which can find the date of the 1st, 2nd, 3rd, 4th or 5th occurrence of any day of the week, in a particular month and year:

Code: Select all

<?php 
function nextdate($ordinal, $dow, $next)  {
  // Returns the date of next occurence of the [ordinal] day of the [next] month--
  //   [ordinal being the 1st, 2nd, 3rd, 4th or 5th
  //   day being Sun - Sat, 0 - 6
  //   next being either the next occurrence (0), the following occurrence (1), etc.
  //   Example: to find the next 2nd Friday after today's date,
  //      nextdate(2,5,0)
 
    // Validate parameters:
    if ($ordinal < 1 | $ordinal > 5 | $dow < 0 | $dow > 6) return 0;
    if ($next=='') $next=0;
    $ordinal--;
    // First, calculate the next occurrence:
    $month=date('m');  // this month
    $year=date('Y');   // this year
    $dt1=mktime(0,0,0,$month,1,$year);   // the 1st of this month
    $dow1=date('w',$dt1);   // day of the week of the 1st of this month
    while ($dow1<>$dow) {  // increment days until 1st dow of this month
        $dt1=$dt1 + 24*60*60;
        $dow1=date('w',$dt1); 
    }
    $dt2=$dt1 + $ordinal*7*24*60*60;  // advance one week for desired occurrence
    if (mktime() > $dt2) {   // we've already passed the second Friday of this month!
        $nextmonth=$dt2 + 30*24*60*60;
        $month=date('m',$nextmonth);  // next month
        $year=date('Y',$nextmonth);   // next year
        $dt1=mktime(0,0,0,$month,1,$year);   // the 1st of next month
        $dow1=date('w',$dt1);   // day of the week of the 1st of next month
        while ($dow1<>$dow) {  // increment days until 1st dow of next month
            $dt1=$dt1 + 24*60*60;
            $dow1=date('w',$dt1); 
        }
    }
    $dt2=$dt1 + $ordinal*7*24*60*60;  // advance one week for desired occurrence
    // Now $dt2 contains the timestamp of the next meeting date
    // If a following occurence was requested, we now have to advance the month:
    if ($next > 0) {
        $dt2 += $next*30*24*60*60;
        // And we once again must find the dow for the first of that month:     
        $month=date('m',$dt2);  // next month
        $year=date('Y',$dt2);   // next year
        $dt1=mktime(0,0,0,$month,1,$year);   // the 1st of next month
        $dow1=date('w',$dt1);   // day of the week of the 1st of next month
        while ($dow1<>$dow) {  // increment days until 1st dow of next month
            $dt1=$dt1 + 24*60*60;
            $dow1=date('w',$dt1); 
        }
        $dt2=$dt1 + $ordinal*7*24*60*60;  // advance one week for desired occurrence        
    }
    return $dtformat=date('l, M j, Y',$dt2);
}
?>
You're welcome to use it or modify it to your own needs. No warranty! :wink:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Calendar - Returning date of the 2nd tuesday of month

Post by califdon »

VladSun wrote:

Code: Select all

function get_second_tuesday($month,$year)
{
    return date("Y-m-d", strtotime("second tuesday", mktime(0, 0, 0, $month, 0, $year)));
}
 
No kidding! :D I never knew you could do that! Never too old to learn!
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Calendar - Returning date of the 2nd tuesday of month

Post by VladSun »

califdon wrote:No kidding! :D I never knew you could do that! Never too old to learn!
:lol: :lol: :lol:
strtotime has become one of my favorite functions ;)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Re: Calendar - Returning date of the 2nd tuesday of month

Post by tecktalkcm0391 »

VladSun wrote:

Code: Select all

function get_second_tuesday($month,$year)
{
    return date("Y-m-d", strtotime("second tuesday", mktime(0, 0, 0, $month, 0, $year)));
}
 
I got this to work EXCEPT when the first day of the month is Tuesday...
UPDATE: also when the first day is Monday.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Calendar - Returning date of the 2nd tuesday of month

Post by VladSun »

Yes, I recall that there are some issues with edge cases... But ...
First day - Tuesday:
2008-01, 2008-04, 2008-08 - it's OK
First day - Monday:
2008-09, 2008-12 - it's OK
I can't see problems.
Can you give me an example?

PS: What version of PHP do you use?
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply