Hi all,
I have little experience with PHP, so I found the following function which returns the two-digit day of the Nth occurrence of a day of the week in a given month/year:
PHP Code:
function date_from_nameday($occurrence, $name_of_day, $month, $year)
{
$one_day = 24 * 60 *60;
$one_week = 7 * $one_day;
$one = mktime(0, 0, 0, $month, 1, $year);
$two = $one + $one_day;
$three = $two + $one_day;
$four = $three + $one_day;
$five = $four + $one_day;
$six = $five + $one_day;
$seven = $six + $one_day;
$first = date('N', $one);
$second = date('N', $two);
$third = date('N', $three);
$fourth = date('N', $four);
$fifth = date('N', $five);
$sixth = date('N', $six);
$seventh = date('N', $seven);
switch($name_of_day)
{
case $first: $first_occurrence = $one; break;
case $second: $first_occurrence = $two; break;
case $third: $first_occurrence = $three; break;
case $fourth: $first_occurrence = $four; break;
case $fifth: $first_occurrence = $five; break;
case $sixth: $first_occurrence = $six; break;
case $seventh: $first_occurrence = $seven; break;
}
$date_of_occurrence = ($occurrence > 1) ? $first_occurrence + ($one_week * ($occurrence - 1)) : $first_occurrence;
$day_to_return = date('d', $date_of_occurrence);
$month_of_day = date('m', $date_of_occurrence);
if ($month_of_day != $month)
return -1;
else
return $day_to_return;
}
And I have used it to find the first Tuesday of each month like this:
PHP Code:
if(time() > mktime(0, 0, 0, date('m'), date_from_nameday(1, 2, date('n'), date('Y')), date('y'))){
echo '<span id="hfb_TickerMonth">'.date('F', mktime(0, 0, 0, date('n')+1, date('j'), date('y'))).'</span>';
echo '<span id="hfb_TickerDay">'.date_from_nameday(1, 2, date('n')+1, date('Y')).'</span>';
} else {
echo '<span id="hfb_TickerMonth">'.date('F').'</span>';
echo '<span id="hfb_TickerDay">'.date_from_nameday(1, 2, date('n'), date('Y')).'</span>';
}
The if statement is checking whether or not today is beyond the first Tuesday, in which case I look for the following month's first Tuesday.
Problem is, now I need it to take the time of day into account as well, and only look for the following month's first Tuesday if today is both beyond the first Tuesday and past 7:00 PM.
How do I go about this? Do I just change the hour parameter from 0 to 19? That was my best guess, and that's what I did, but don't know if it did the trick or not.
Thanks!
Bookmarks