note 12499 deleted from ref.datetime by cece

From: Date: Wed, 20 Aug 2003 13:44:12 +0000
Subject: note 12499 deleted from ref.datetime by cece
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-54752@lists.php.net to get a copy of this message
Note Submitter: mmotley@la-mirada.nospam.net ---- Some useful functions: /* dayofweek() will return the day of the week a given date falls. 0=Sunday, 1=Monday, etc. */ 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 is a mod function that deals with negative numbers properly, used by the nthDayOfMonth function below. */ 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 [like 2001] * * Returns: * The date, in the month you passed, that fits the criteria. * or.. * -1 = invalid date * -2 = There is no Nth day of that month, like no 5th Tuesday * of the specified month */ 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); /* Sanity check */ if (!checkdate($month,$retval,$year)) return -2; return $retval; }

« previous php.notes (#54752) next »