slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know how I could get the week number (1st week of this month, 2nd week of this month ...) of a given date.

I searched around and found the following code that gives me the current week number of the year, but I need to know what week of the month it is. Any help would be appreciated.

#!perl # Parse time string ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); # Calculate number of full weeks this year $week = int(($yday+1-$wday)/7); # Add 1 if today isn't Saturday if ($wday < 6) {$week +=1;} print "Week $week\n";

Replies are listed 'Best First'.
Re: Getting the current week number
by NetWallah (Canon) on Jun 20, 2004 at 05:18 UTC
    From Date::Calc doc:

    5) How do I calculate the number of the week of month the current date lies in?

                April 1998
        Mon Tue Wed Thu Fri Sat Sun
                  1   2   3   4   5  =  week #1
          6   7   8   9  10  11  12  =  week #2
         13  14  15  16  17  18  19  =  week #3
         20  21  22  23  24  25  26  =  week #4
         27  28  29  30              =  week #5
    
    use Date::Calc qw( Today Day_of_Week ); my ($year,$month,$day) = Today(); my $week = int(($day + Day_of_Week($year,$month,1) - 2) / 7) + 1;

    Offense, like beauty, is in the eye of the beholder, and a fantasy.
    By guaranteeing freedom of expression, the First Amendment also guarantees offense.

Re: Getting the current week number
by Zaxo (Archbishop) on Jun 20, 2004 at 05:28 UTC

    POSIX::strftime can simplify that - the %U, %V, %W formats give the week of the year by different reckonings. How about subtracting the week of the year for the first of the month from the current week of the year?

    #!/usr/bin/perl use POSIX 'strftime'; my @now = localtime; my @first = @now; $first[3] = 0; print strftime('%V',@now) - strftime('%V',@first), $/ __END__ 3
    There are lots of ways to count weeks, depending on when you start. The strftime formats label them from 00 to 53. %U counts the first sunday of the year as the start of week 01. %V follows ISO 8601:1988 and calls week 01 the first with at least four days. %W is like %U, but counts the first Monday as the start of week 01.

    If you want the weeks to start at 1 on the first of the month, you can just figure it from the day of the month: print 1 + int((localtime)[3]/7),$/;

    After Compline,
    Zaxo

Re: Getting the current week number
by davido (Cardinal) on Jun 20, 2004 at 05:12 UTC

    That's tricky because of the ambiguity of what is meant by the first week of the month. Do you want to count partial weeks too? For example, if the month starts on a Monday (instead of a Sunday), do you want to count that first Monday-Saturday to be the first week? And if you do, will you also count months that start on a Thursday as having their first week be Thursday-Saturday?.... ...and, if you do, what if the first day of the month is a Saturday? Does that mean that week one of the month consists of one day only?

    Once you work out the details that define the problem, you'll be well on your way to working out the details of implementing the solution.

    And you also have to consider whos calendar you're using. When I worked for one corporation, the "Fiscal" calendar was quite different from the traditional calendar. We were on what we called a "4-5-5-4" schedule, meaning the "Months" all had exactly 4 or 5 weeks in them; exactly. Fiscal months always started on Sunday, and always ended on Saturday, no matter what the traditional calendar said. And that meant that June, for this corporation, might not be over until some time in the first week of what the rest of the world called July.

    I've looked at Date::Manip and see that, though it does provide a Week-of-the-year function, it doesn't offer week-of-the-month. However, once you define your problem more tightly, you might have success using the Date_WeekOfYear() function and counting back to the first of the current month.


    Dave

Re: Getting the current week number
by matija (Priest) on Jun 20, 2004 at 05:22 UTC
    Whenever I need some operation involving dates, I immediately turn to Date::Manip. In this case, I think the code would go something like this:
    $secs=time; $date = &ParseDateString("epoch $secs"); $week=UnixDate($date,"%W");
    Note that you have:
    %U week of year, Sunday as first day of week - 01 to 53 %W week of year, Monday as first day of week - 01 to 53
Re: Getting the current week number
by blokhead (Monsignor) on Jun 20, 2004 at 05:19 UTC
    "Week of the month" is a bit ambiguous. Do you count partial weeks (like June 1 - June 5 of this year)? Do you start on Sunday or Monday? Are you looking for how many 7-day periods were before this date, ignoring Sunday-alignment (probably not since that's trivial)?

    If you mean "which column of the calendar does this date go on, if weeks start with Sunday", then this will do:

    use POSIX 'ceil'; my $week = 1 + ceil( ($d - $wday - 1) / 7 );
    From my basic testing, it seems to work for May (which starts on a Saturday), August (which starts on a Sunday), and June (which starts on a weekday) of this year.

    blokhead

Re: Getting the current week number
by fglock (Vicar) on Jun 20, 2004 at 05:17 UTC

    If the code works with year days, it should work with month days as well:

    # Calculate number of full weeks this month $week = int(($mday+1-$wday)/7); # Add 1 if today isn't Saturday if ($wday < 6) {$week +=1;} print "Week $week\n";