Facebook   Twitter    YouTube    RSS Feed    Android App    iPhone and iPad App     BlackBerry App    
Subscribe to Newsletter



Results 1 to 15 of 15

Thread: Calculating the week in the month (PHP)

  1. #1
    Super Grandmaster Nod's Avatar
    Join Date
    Jul 2005
    Location
    Cape Town
    Posts
    6,990

    Default Calculating the week in the month (PHP)

    So there is a function for the day of the week (date('d')), the total days in a month (date('t')) and the week in the year, but how can I get the week number (1-4/5) in the month?

    Any help would be appreciated.

  2. #2

    Default

    Why don't you write your own function. You must put on your thinking cap though and fail a couple of dozen times depending on your experience.

    Happy coding...
    Ho$H

  3. #3
    Super Grandmaster Nod's Avatar
    Join Date
    Jul 2005
    Location
    Cape Town
    Posts
    6,990

    Default

    Thats what I was trying to do, but the maths involved have bested me. I can't get a reliable result.

  4. #4

    Default

    mail me what you have done so far so that I can have a look see if I can help in any way.
    Ho$H

  5. #5
    Grandmaster Kloon's Avatar
    Join Date
    Nov 2006
    Location
    172.0.0.0/8
    Posts
    1,544
    Blog Entries
    2

    Default

    PHP Code:
    function getWeekNoByDay($year 2007,$month 5,$day 5) {
           return 
    ceil(($day date("w",mktime(0,0,0,$month,1,$year)))/7);   
       } 
    and if you want the the week in the currentmonth devide result by month eg 5 here. Hope that helps you

  6. #6
    Super Grandmaster Nod's Avatar
    Join Date
    Jul 2005
    Location
    Cape Town
    Posts
    6,990

    Default

    Quote Originally Posted by Kloon View Post
    PHP Code:
    function getWeekNoByDay($year 2007,$month 5,$day 5) {
           return 
    ceil(($day date("w",mktime(0,0,0,$month,1,$year)))/7);   
       } 
    and if you want the the week in the currentmonth devide result by month eg 5 here. Hope that helps you
    Does not work for me. If I give it the current year, month and the 15th, it gives me the week of the month as 2. Did the divide by month as well. Shouldn't the result be 3?

  7. #7
    Grandmaster Kloon's Avatar
    Join Date
    Nov 2006
    Location
    172.0.0.0/8
    Posts
    1,544
    Blog Entries
    2

    Default

    2 would be correct, it is only the 12th. This means your in the second week of feb, or so i think, if you think it should be 3 try rounding the value up

  8. #8
    Grandmaster Kloon's Avatar
    Join Date
    Nov 2006
    Location
    172.0.0.0/8
    Posts
    1,544
    Blog Entries
    2

    Default

    Sorry mate didnt check the code properly, no need to devide by month it already gives you the correct answer. If i run it with todays date i get 3

  9. #9
    Super Grandmaster Nod's Avatar
    Join Date
    Jul 2005
    Location
    Cape Town
    Posts
    6,990

    Default

    I'll have to speak to the guy that wrote the spec, because it seems that he only caters for 4 weeks in a month, which there aren't.

    Thanks for all the help. It is appreciated.

  10. #10

  11. #11
    Super Grandmaster Nod's Avatar
    Join Date
    Jul 2005
    Location
    Cape Town
    Posts
    6,990

    Default

    Quote Originally Posted by Kloon View Post
    Nope there can be 5 weeks in a month too
    I'm aware of that, yes.

  12. #12

    Default

    Why over-complicate things? I googled this very thing (it's how I found your question) and it seems anyone who has "solved" this is generating like 2000 lines of code to do something so simple.

    Here's my idea on how this should be done...

    Code:
    // Code by RobbieF.com - determines current week of the month
    // March 5, 2007
    $i=0;
    $week=0;
    if (date("N", mktime(0, 0, 0, date(n), 1, date(Y))) <= "6") $week++;
    while ($i <= date(j)) {
    if (date("N", mktime(0, 0, 0, date(n), $i, date(Y))) == "7") $week++;
    $i++;
    }
    
    echo "Current week of the month: " . $week;
    All that does is simply count how many Sundays have passed (the "7") since the beginning of the month, and also checks if the first day of the month falls on or before Saturday (which would cause it to be week 1... this solves the "week 5" thing). That makes sense to me to do what you're wanting. I dunno why we'd wanna over-complicate something like that.

    I haven't really tested this, and it's just a quick mockup - but it should at least give you a good starting point... but it should work.

    Hope it helps!!
    www.RobbieF.com
    Last edited by djRobbieF; 05-03-2007 at 05:00 PM.

  13. #13

    Default

    Actually there's an easier way.
    The date function can return week number from a timestamp. Use mktime() to format your date as a UNIX timestamp.
    eg:
    date ("W",mktime(0, 0, 0, 3, 22, 2007));
    where 3 is month, 22 is day and 2007 is year.
    Hope this helps.
    Last edited by Ratsalad; 23-03-2007 at 11:47 AM.

  14. #14

    Default

    This line of code will return the week number. Note: PHP's "W" parameter in the Date function assumes that Monday is the first day of the week, so if you need to work from Sunday as the first day of the week, you'd need to build that logic in yourself.
    Code:
    $weekNum = date("W") - date("W",strtotime(date("Y-m-01"))) + 1;
    We add the 1 at the end because the "W" parameter returns a zero based result, so if you leave off the + 1, then you must remember the first week will be returned as 0.

  15. #15

    Default

    function getWeekOfTheMonth($YY,$MM,$DD){
    $weekNum = date("W",mktime(0,0,0,$MM,$DD,$YY)) - date("W",mktime(0,0,0,$MM,01,$YY)) + 1;
    return $weekNum;
    }

Bookmarks

Bookmarks

Posting Permissions

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