Page 1 of 1

Validate week number is withing given month

Posted: Fri Nov 11, 2005 4:22 am
by jmut
How could I validate that a given week

week number 43(produced by date('W'))
is really within

month x and year y

example:
week 45
it is true that it is in november....probably no matter of the year.
The year will only matter on leap years...not even sure if so.


But:

week 12 is not in month november for sure..


So how can I validate if the week number is within given month.


I was thinking .....taking week number for the firstday of the month
then week number for the last one....

and see if it within these

But taking the last day of the month I am a bit lost...

Thanks for any advice

Posted: Fri Nov 11, 2005 7:17 am
by jmut
Problem solved...I think.
If someone is interested...here is the solution.
10x

Code: Select all

//taken from php.net user comments
function mod($a,$b) {
   $x1=(int) abs($a/$b);
   $x2=$a/$b;
   return $a-($x1*$b);
}
//taken from php.net user comments
function IsLeapYear($year)
{
    $y= $year;
    $bulis=((mod($y,4)==0) && ((mod ($y,100)<>0) || (mod($y,400)==0)));
    return $bulis;
}
//taken from php.net user comments --with little fix
function daycount($month,$year)
{
    $dc_year    = $year;
    $dc_month   = $month;

    switch ($dc_month)
    {
        case  1:
        case  3:
        case  5:
        case  7:
        case  8:
        case 10:
        case 12:
               return 31;
           break;
        case  4:
        case  6:
        case  9:
        case 11:
               return 30;
           break;
        case 2:
           if (IsLeapYear($year)) { return 29; } else { return 28; };
           break;
    }
}


function isValidWeek($week_calendar,$month_calendar,$year)
{
    switch (true) {
    	case (!preg_match('%^\d{1,2}$%',$week_calendar)) :
                return false;
    		break;

    	case (!preg_match('%^\d{1,2}$%',$month_calendar)) :
                return false;
    		break;

    	case (!preg_match('%^\d{4}$%',$year)) :
                return false;
    		break;

    	default:
    		break;
    }

    $fstDayMonth = 1;
    $lstDayMonth = daycount($month_calendar,$year);

    $fstWeekMonth = date("W",mktime(0, 0, 0, $month_calendar, $fstDayMonth, $year));

    $lstWeekMonth = date("W",mktime(0, 0, 0, $month_calendar, $lstDayMonth, $year));

    return ( ($week_calendar >= $fstWeekMonth) && ($week_calendar <= $lstWeekMonth) );

}

Posted: Fri Nov 11, 2005 7:27 am
by Jenk
You can use

Code: Select all

date('L', $timestamp);
to determine if the given year is a leapyear (1 = leap year, 0 = non leap year) :)

Posted: Fri Nov 11, 2005 7:32 am
by jmut
Jenk wrote:You can use

Code: Select all

date('L', $timestamp);
to determine if the given year is a leapyear (1 = leap year, 0 = non leap year) :)
haha :)

thanks for this one....well there is always new stuff to learn....I just have never played long enough with dates.