Hello Community,
this is not a question but a suggestion that works for me:
I spent about 2 days to do this, but I needed to generate an array of weeks in a year or between dates.
As this does't work properly using the standard functions, I wrote my own at the end.
So this is the comeout of my efforts to find out a weeknumber using the gregorian calendar-system in almost any european country:
function get_WeekOfYear( $day, $month, $year )
{
$date = getdate( mktime( 0, 0, 0, $month, $day, $year ) );
//if you want to override daylight saving times
//$date = getdate( mktime(9, 0, 0, $month, $day, $year ) );
$jan1 = getdate( mktime( 0, 0, 0, 1, 1, $year ) );
//Sunday in US - System is 0, but we need it to be 7, so last day of the week
if($date['wday'] == 0)
$datewday = 7;
else
$datewday = $date['wday'];
$dateyday = $date['yday'];
if($jan1['wday'] == 0)
$jan1wday = 7;
else
$jan1wday = $jan1['wday'];
$jan1yday = $jan1['yday'];
//check if year is a leap-year
if ( ( $year % 4 == 0 && $year % 100 != 0 ) || $year % 400 == 0 ) {
$s = 1;
} else {
$s = 0;
}
//check how many weeks the year has
if ( ( $jan1['wday'] == 3 && $s == 1 ) || $jan1['wday'] == 4 ) {
$weeks = 53;
} else
$weeks = 52;
$weeknr = floor( ( $dateyday + ( 7 - $datewday ) + $jan1wday ) / 7 );
if ( $jan1yday <= 7 - $jan1wday && $jan1wday > 3){
if($weeks == 52)
$weeknr--;
}
return $weeknr;
}
As you may know, the 1st day of the week in european countries (at least in Germany) start with a Monday, in the US with a Sunday. Else if the first day of the week is a Wednesday and the year is a leap - year or if the first day of the year is a Thursday, the year has 53 weeks, else it has 52 weeks. Further week 1 in most european countries is considered to be the first week that has 4 or more days within the first 7 days of the year, else the week is week 0. The function above considers all these aspects, and will deliver the accurate week of the year within the beginning of 1970 and end of 2037 (because of the UNIX timestamp issues), as far as I have been able to test it.
Hope this helps a few people.
Greetings
Gaucho