Page 1 of 1

Second Tuesday of Month

Posted: Tue Oct 12, 2004 2:01 pm
by colmtourque
Does anyone know of a good tutorial on how to have php give you the second tuesday of a month.
(well really an specefic day of a month, then I can adjust).
I am trying to figure this out, and rather than be given the code, I'd really like to learn how.
I'm gonna keep googling.

Posted: Tue Oct 12, 2004 3:10 pm
by Weirdan
[php_man]date[/php_man]
[php_man]mktime[/php_man]

Code: Select all

$first_day = mktime(0,0,0,date('n'), 1, date('Y')); // first day of the month
$wd_first_day = date('w', $first_day); // week day of the first day
$add = 3 + ( ($wd_first_day<2) ? -$wd_first_day : 7-$wd_first_day); // +/- adjust
$sec_tuesday = $add + 14; // day of the month of the second tuesday
$sec_tuesday_ts = mktime(0,0,0, date('n'), $sec_tuesday, date('Y')); // day of the month of the second tuesday (timestamp) 
$readable_tuesday = date('r',$sec_tuesday_ts); // string representation
echo $readable_tuesday; // output

Posted: Tue Oct 12, 2004 3:22 pm
by Weirdan
or simply

Code: Select all

$month_offset = -2;
echo date('r', strtotime('2 tuesday ' . $month_offset . ' month', mktime(0, 0, 0, date('n'), 1, date('Y') ) ) );
[php_man]strtotime[/php_man]
[php_man]date[/php_man]
[php_man]mktime[/php_man]