note 38208 deleted from function.date by kennyt
| From: | kennyt@php.net | Date: | Sun, 18 Jul 2004 17:29:46 +0000 |
| Subject: | note 38208 deleted from function.date by kennyt | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73517@lists.php.net to get a copy of this message | ||
Note Submitter: zombie(at)artattack(dot)to
----
I had to write a function at work to get the first day of classes at the U of M for a given semester
for a given year. Fall semester starts the day after labor day and spring semester starts the day
after Martain Luther King Jr. Day.
Givin that labor day is the first monday of september, and that MLK Jr day is the 3rd monday in
January, I threw together a super quick function to get the actual day of the month.
The function uses a quick sort style short circuiting. Therefore, there is never more than 3
iterations. Makes it super fast.
This function could easily be modified to get the $n $dayname of $month of $year
function get_first_day_of_classes($term_string, $year){
//note that 2 stands for Tuesday, if you need any other day, then replace 2 with the week index
(sunday = 0)
//term
if ($term_string == 'fall'){
$n = 1; //1st
$month = 9;
}//looking for first tuesday of month (tuesday after labor day)
else {
$n = 3; //1st
$month = 1;
}
$date_day = 1;
while($matches < $n){
echo date('l dS of F Y', mktime(0,0,0,$month,$date_day,$year)) . '<BR>';
$weekday_i = date('w', mktime(0,0,0,$month,$date_day,$year));
if ($weekday_i == 2){ //tuesday...
echo '<strong>HOT</strong><BR>';
$matches++;
if ($matches == $n){ //quick short circuit
return $date_day;
}
$date_day += 7;
}
else { //jump closer to save time...
$date_day += ($weekday_i > 2) ? (7 - ($weekday_i - 2)) : (2 - $weekday_i);
//$date_day = $date_day - 1; //since we will increment above
}
}
return $date_day;
}
echo '(' . get_first_day_of_classes('spring', 2004) . ')';
Blaine Flynn Garrett
Webmaster of the art attack
http://artattack.to?zombie