note 58112 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 11 Jun 2006 11:39:23 +0000 |
| Subject: | note 58112 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-114239@lists.php.net to get a copy of this message | ||
Note Submitter: paulhan at eircom dot net
----
Same day in week each month script
This is a script I use to find dates based on first or third Friday in every month (or any other
day in a month)
<?php
//For this example, give nextdate a timestamp
$nextdate = time();
//Format nextdate for viewing
$output = date("D d-F-Y H:i", $nextdate)."<br />\n";
//Initialise no of days in month array
$monArr=array(31,28,31,30,31,30,31,31,30,31,30,31);
//Get the next twelve months (can be more or less)
for ($i=0; $i<12; $i++){
//Convert timestamp into its constituents
$dt = getdate($nextdate);
//Get no of days in the month
$nod = $monArr[$dt['mon']-1];
//Do a simple leap year check. Better example elsewhere
if ($dt['mon']==2 && ($dt['year']%4) == 0){
$nod = 29;
}
//Get the day part of the time
$dom = $dt['mday'];
//Get earliest possible date for that day
$firstday = $dom % 7;
//Get the difference between the number of days and the
// maximum number of possible weeks in a month
$diff = $nod - 28;
//If the earliest possible date falls within that difference
// and is greater than 0, and the date is less than 28,
// then add 35 days using strtotime to get over DST
//issues. Just means there will be five of those days in
// the month
if ($firstday <= $diff && $firstday > 0 && $dom < 28){
$nextdate = strtotime("+35 days",$nextdate);
//If not, just add 28 days to the date
}else{
$nextdate = strtotime("+28 days",$nextdate);
}
//Show the output for checking, could also use the raw
// timestamp for a mysql query
$output .= date("D d-F-Y H:i", $nextdate)."<br />\n";
//Should output the third Friday in every month, for
// instance
echo $output;
}
?>