#native_company# #native_desc#
#native_cta#

Day of week functions

By Mark Motley
on April 17, 2001

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: Contains two functions, dayofweek() will return the day of the week for a given date (0=Sunday, 1=Monday, etc). nthDayOfMonth() will return the date of the nth weekday of a month (i.e. 2nd Tuesday of April 2001 will return 10).

/*
 * PHP Day of Week functions
 * Copyright (c) 2001, Mark Motley <[email protected]>
 * GNU Public license, use as you want
 */

/*
 * dayofweek($day,$month,$year) will compute the day of the week a
 * a particular date falls.
 * 
 * Paramaters:
 *
 *   $day - Day of month [1-31]
 *   $month - Month [1-12]
 *   $year - The year [1900-?]
 *
 * Returns:
 *
 *   Numerical day of week where 0=Sunday, 1=Monday, etc.
 *   On error (out-of-bounds dates) returns -1.
 *
 */

function dayofweek($day,$month,$year) {

        /* Check date for validity */
        if (!checkdate($month,$day,$year))
                return -1;

        $a=(int)((14-$month) / 12);
        $y=$year-$a;
        $m=$month + (12*$a) - 2;

        $retval=($day + $y + (int)($y/4) - (int)($y/100) + (int)($y/400) + (int)((31*$m)/12)) % 7;

        return $retval;
}

/* phpdow_mod($a,$b) is a fixed-up modulo function that will deal with
 * negative numbers properly. PHP's modulo function doesn't, and the
 * forumulas are dependent on it.
 * It's really only here to support the nthDayOfMonth() function.
 */

function phpdow_mod($a,$b) {
        if ($a <= 0)
                return (int)phpdow_mod($b-abs($a),$b);
        else
                return (int)($a%$b);
}


/*
 * nthDayOfMonth($n,$dow,$month,$year) will compute the Nth day of the given
 * month.  For example, the first Monday in April 2001.
 *
 * Parameters:
 *
 *  $n - the Nth day you want, i.e. 2 for 2nd 
 *  $dow - The day of week you want, 0=Sunday, 1=Monday, etc. [0-6]
 *  $month - The month you want [1-12]
 *  $year - The full year [1900? - ??]
 *
 * Returns:
 * 
 *  The date, in the month you passed, that fits the criteria.
 *  Here we return to error conditions:
 *    -1 = invalid date
 *    -2 = There is no Nth day of that month, like no 5th Tuesday
 *         of the specified month
 *
 * Example:
 *
 *  To find the first Tuesday in June 2001:
 *     $day = nthDayInMonth(1,2,6,2001);
 *  will return 4, which means the first Tuesday in June is 6/4/2001.
 *
 */

function nthDayOfMonth($n,$dow,$month,$year) {

        /* Check the date */
        if ($month > 12) 
                return -1;

        if ($dow > 6)
                return -1;

        /* Valid Nth day, should be no more than 5 */
        if (($n <= 0) || ($n > 5))
                return -1;

        $retval = (7*$n)-6+phpdow_mod($dow-dayofweek(1,$month,$year),7);

        /* Make sure the date is not greater than the number of days
           in the month */
        if (!checkdate($month,$retval,$year))
                return -2;

        /* Otherwise we return the date */
        return $retval;
}