Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get number of weeks for a month 1

Status
Not open for further replies.

eva0212

Programmer
Jun 30, 2008
8
Well... how to get number of weeks for a month that for a selected year? is does anybody know how to do this?
 
Do you already have code?
If so could you post it?

I am a progammer.... no realy.
 
The cal_days_in_month function would be a useful starting point.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
actually, i think we need some more information first. what do you mean by 'number of weeks'? do you mean 'number of whole weeks'? starting on what day?

so you have a variety of solutions:

1. for a fractional result take cal_days_in_months and divide by 7.
2. for a full week based solution try this code

Code:
function numWeeks($year, $month, $start=0){
	$unix = strtotime("$year-$month-01");
	$numDays = date('t', $unix);
	if ($start===0){
		$dayOne = date('w', $unix); // sunday based week 0-6
	} else {
		$dayOne = date('N', $unix); //monday based week 1-7
		$dayOne--; //convert for 0 based weeks
	}
	
	//if day one is not the start of the week then advance to start
	$numWeeks = floor(($numDays - (6 - $dayOne))/7);
	return $numWeeks;
}

if you are looking for a calendar plotting script then i posted one a few days ago on rathercurious.net
 
Number of week here mean total number of week in a month. I use code below to get get it. Is it correct?

Code:
$Total1=max(date('W', mktime(0,0,0,1,25,$year)), date('W', mktime(0,0,0,1,31,$year)));
$Total2=max(date('W', mktime(0,0,0,2,25,$year)), date('W', mktime(0,0,0,2,31,$year)));
$Total3=max(date('W', mktime(0,0,0,3,25,$year)), date('W', mktime(0,0,0,3,31,$year)));
$Total4=max(date('W', mktime(0,0,0,4,25,$year)), date('W', mktime(0,0,0,4,31,$year)));
$Total5=max(date('W', mktime(0,0,0,5,25,$year)), date('W', mktime(0,0,0,5,31,$year)));
$Total6=max(date('W', mktime(0,0,0,6,25,$year)), date('W', mktime(0,0,0,6,31,$year)));
$Total7=max(date('W', mktime(0,0,0,7,25,$year)), date('W', mktime(0,0,0,7,31,$year)));
$Total8=max(date('W', mktime(0,0,0,8,25,$year)), date('W', mktime(0,0,0,8,31,$year)));
$Total9=max(date('W', mktime(0,0,0,9,25,$year)), date('W', mktime(0,0,0,9,31,$year)));
$Total10=max(date('W', mktime(0,0,0,10,25,$year)), date('W', mktime(0,0,0,10,31,$year)));
$Total11=max(date('W', mktime(0,0,0,11,25,$year)), date('W', mktime(0,0,0,11,31,$year)));
$Total12=max(date('W', mktime(0,0,0,12,25,$year)), date('W', mktime(0,0,0,12,31,$year)));

$TotalWeekJan=$Total1;
$TotalWeekFeb=$Total2-$Total1;
$TotalWeekMar=$Total3-$Total2;
$TotalWeekApr=$Total4-$Total3;
$TotalWeekMay=$Total5-$Total4;
$TotalWeekJun=$Total6-$Total5;
$TotalWeekJul=$Total7-$Total6;
$TotalWeekAug=$Total8-$Total7;
$TotalWeekSep=$Total9-$Total8;
$TotalWeekOct=$Total10-$Total9;
$TotalWeekNov=$Total11-$Total10;
$TotalWeekDec=$Total12-$Total11;

 
no, it does not appear to be correct although you have still failed to explain properly what you mean by total number of weeks in a month.

i have provided you with the two possible solutions above (assuming a gregorian calendar).
 
Is mean that example for January 2008, i have 5 week in this month. I want that can generate this number of week according for the year. Thanks jpadie. I will try the solution you give.
 
i still do not think that you have provided us with the algorithm for determining what is a 'week' for a month.

are you saying that if the first day of a month were a saturday (say) and that month has 31 days, then you would have 6 weeks for that month?

please can you explain _why_ you need this returned? that may well help us understand you.
 
Actually i want to get total week for a year first. Then from here i need to count number of week for a month accordingly. This is use to generate an annual report.
 
eva0212 said:
Actually i want to get total week for a year first.
Assuming a Gregorian calendar, there are always 365 or 366 days in a year. Therefore, the total number of weeks in a year is either 52.143 (365 / 7) or 52.286 (366 / 7). I suggest you read the following sources:
Year - Wikipedia
Gregorian Calendar - Wikipedia

You still haven't answered the following questions jpadie initially asked you:
jpadie said:
do you mean 'number of whole weeks'?
For example, "4 weeks" rather than "4.43 weeks"

jpadie said:
starting on what day?
Sunday/Monday/other?

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Sorry cause no so undestanding Gregorian calendar. Just undestand after read the articles. I mean is numbers of whole weeks. The day should be starting on Sunday.
 
so is your algorithm that a week is in a month only if
1. the sunday falls within a month; AND
2. the immediately following Saturday also falls within the same month


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top