Validate week number is withing given month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Validate week number is withing given month

Post 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
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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) );

}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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) :)
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post 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.
Post Reply