Page 1 of 1

Calendar - Returning date of the 2nd tuesday of month

Posted: Sun Jun 08, 2008 6:09 pm
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!

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

Posted: Sun Jun 08, 2008 6:19 pm
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)));
}
 

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

Posted: Sun Jun 08, 2008 6:26 pm
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:

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

Posted: Sun Jun 08, 2008 6:29 pm
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!

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

Posted: Sun Jun 08, 2008 6:32 pm
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 ;)

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

Posted: Mon Jun 09, 2008 2:12 pm
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.

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

Posted: Mon Jun 09, 2008 3:57 pm
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?