Second 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
colmtourque
Forum Newbie
Posts: 20
Joined: Wed Aug 25, 2004 11:59 am

Second Tuesday of Month

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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]
Post Reply