note 58112 added to function.date
| From: | paulhan at eircom dot net | Date: | Mon, 24 Oct 2005 20:03:34 +0000 |
| Subject: | note 58112 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97529@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58112
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58112&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58112&report=yes
Search -- http://master.php.net/manage/user-notes.php