note 38208 added to function.date
| From: | zombie at rn2 dot php dot net | Date: | Thu, 11 Dec 2003 22:29:03 +0000 |
| Subject: | note 38208 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61837@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38208
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38208&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38208&report=yes
Search -- http://master.php.net/manage/user-notes.php