note 29089 added to function.date
| From: | brian at bender-high dot com | Date: | Sat, 01 Feb 2003 18:40:14 +0000 |
| Subject: | note 29089 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-43262@lists.php.net to get a copy of this message | ||
We have meetings once a month. For instance, one might
be on the third Thursday of each month ... or the first
Monday, etc. I wanted a function to return the date
of next month's meeting given the date of this month's
meeting.
Here are a couple of functions that do that:
<h1>Find the next meeting date</h1>
<?
/* Convert date in YYYYMMDD format to unix format */
function date_to_epoch ( $d ) {
return mktime ( 3, 0, 0, substr ( $d, 4, 2 ), substr ( $d, 6, 2 ),
substr ( $d, 0, 4 ) );
}
/* Find the next meeting day, assuming you want to
meet one month later on the same day of the week
and the same week of the month. */
function next_month($d) {
// Find where we are in the month
$day_of_week = date("w", $d);
$week_of_month = ceil(date("d", $d)/7);
// Find first day of next month
$d2 = date_to_epoch ( date("Ymd",
mktime (0,0,0,date("m",$d)+1,1,date("Y",$d))));
// Find week adjustment factor
$day_of_week2 = date("w", $d2);
$week_adjustment = $week_of_month;
if ($day_of_week >= $day_of_week2) { $week_adjustment -= 1; }
// Find day adjustment factor
$day_adjustment = $day_of_week - $day_of_week2;
// Find the target day
$dy2 = (($week_adjustment) * 7) + $day_adjustment + 1;
// Get month and year to assemble new date variable to return
$mo2 = date("m", $d2);
$yr2 = date("Y", $d2);
// Show calendar to enable a visual check of this function
// $cal = shell_exec("cal -3 $mo2 $yr2");
// echo "<pre>$cal</pre>";
// Return date in YYYYMMDD format
return date("Ymd", mktime (0,0,0,$mo2,$dy2,$yr2));
}
/* Main rountine */
if ($start_date) {
echo "Current Meeting is: $start_date<br/>\n";
$start = date_to_epoch($start_date);
$next_meeting = next_month($start);
echo "Next Meeting is: $next_meeting<br/>\n";
}
?>
<p></p>
<form action=<?=$PHP_SELF?> method="get">
<input type="text" name="start_date"\
value="<?=$next_meeting?>"> Format = YYYYMMDD<br/>
<input type="submit" name="submit" value="Submit"\>
</form>
--
http://www.php.net/manual/en/function.date.php
http://master.php.net/manage/user-notes.php?action=edit+29089
http://master.php.net/manage/user-notes.php?action=delete+29089
http://master.php.net/manage/user-notes.php?action=reject+29089