Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Get current week number?

    Hello,

    I'm trying to make what i thought would be a simple function. but i'm a bit stuck at the moment.

    I've found this code on php.net:

    PHP Code:
    $year date("Y"$date);
    $month date("m"$date);
    if( (isset(
    $_GET['year'])) && (intval($_GET['year']) > 1582) ){
        
    $year intval($_GET['year']);
    }
    if( (isset(
    $_GET['month'])) && (intval($_GET['month']) >= 1) && (intval($_GET['month']) <= 12) ){
        
    $month intval($_GET['month']);
    }
    $date mktime(111$monthdate("d"), $year);
    $first_day_of_month strtotime("-" . (date("d"$date)-1) . " days"$date);
    $last_day_of_month strtotime("+" . (date("t"$first_day_of_month)-1) . " days"$first_day_of_month);
    $first_week_no date("W"$first_day_of_month);
    $last_week_no date("W"$last_day_of_month);
    if(
    $last_week_no $first_week_no$last_week_no date("W"strtotime("-1 week",$last_week_no)) + 1;
    $weeks_of_month $last_week_no $first_week_no 1;
    echo 
    $weeks_of_month
    which will get the number of weeks in the current month.
    but I'm having issues figuring out the best way to find the currently week of month. for example, right now it's November week 3.

    again seems like this should be simple. but i'm just not seeing the logic yet.

    cheers,
    -Ken

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Some considerations ...

    partial weeks or full weeks?
    does your week start on Sunday or Monday?

    you found your script on the internet ...
    did you find it here (in the examples)?
    http://us2.php.net/date

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    Yes, it was in the example on php.net's manual.
    also the week start should be Sunday.

    and like in my example, these would be partial weeks. since the other way wouldn't make much sense.

    "right now it's November week 3"

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    also the week start should be Sunday.
    I never understood how a week can start on Sunday since the weekend is Saturday and Sunday.

  • #5
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Because the week ends are on either side of the week. Like bookends.

  • #6
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I smell a conspiracy.

  • #7
    New Coder
    Join Date
    Apr 2007
    Posts
    89
    Thanks
    1
    Thanked 2 Times in 2 Posts
    PHP Code:
    $day date("m"$date);
    $num $day 7;
    $foo round($num);

    echo 
    "this is week number" $foo "in" $not_yet_definded_month_foo 
    what do you mean by third week in November? if this wasn't a discussion about php I would assume you meant full week (give or take a day or so hence round())

  • #8
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Look at your calendar hanging on your wall ...

    S M T W T F S

    The work-week starts on Monday,
    but the calendar week starts on Sunday.

    I know this argument could go on forever,
    that's why I specifically asked him when he
    wanted to start his week (not mine).

    So, Dec 2, 2007 is the 2nd week of December?

    Is that correct?

    I know it sounds sort of smart-alec of me to ask
    it again, but this type of thing can really cause a
    lot of confusion.

    To me, Dec 2, 2007 is the 1st week of December.
    Not because of the Sunday start thing, but because
    it is the starting of the 1st FULL WEEK of December.

    So, you need to clarify when week 1 is being determined
    for each month. Perhaps if you had an array set-up that
    would determine "week 1" for the next 10 years (120 elements
    in the array), that would really help you out, and it would
    eliminate the problems like Dec. 2, 2007.

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    The system with week numbers is kinda different in different parts of the world. The ISO system follows the European Norm, which is different from the system used in the USA or the UK, for instance. In the systems used in the UK and the USA a week can be both the last week of one year and the first week of the next year. In the ISO system, a week can only belong to a single year.
    Also the calendars differ in what day starts the week, and which days are always guaranteed to be part of the same year. The ISO and UK systems start with a Monday, the system used in the USA starts on a Sunday. The ISO standard, at least four days of the first week week are always part of the first week of the year (or in other terms, the first week whose days belong mostly in the new year). That means that the first Thursday of the year is guaranteed to be in the first week of the year. In the UK system, the first week is always the week containing January 1. Dito in the system used in the USA, the first week is always the week containing January 1. These two systems differ by which day they consider the first of the week, however.

    So, it was a relevant question - not only asking which day the week should start with, but also asking what is considered the criteria for a week belonging to a certain year/month etc. Also relevant is asking whether the numbers may overlap. Say one week has three days at the end of one month/year and four days the next month/year. Is that week the last week of the previous month/year or the first week of the next month/year, or maybe it's both?
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •