Get Week's Starting date
Page 1 of 3 123 LastLast
Results 1 to 15 of 32

Thread: Get Week's Starting date

  1. #1
    Junior Member
    Join Date
    May 2001
    Posts
    25

    Get Week's Starting date

    Can i get the starting date of the week (as monday) with just 2 parameters available, Week number and Year?
    Can anybody help me with this?
    Thanks in advance.


    Bhavesh

  2. #2
    Happy suntra's Avatar
    Join Date
    Sep 2002
    Location
    Montréal (Canada)
    Posts
    877
    Do you want to get "monday" or the date (like last monday, december 30th) ??
    Nzau2006 (XP Pro) :
    Apache 2.0.59 // PHP 5.1.6 // MySQL 5.0.24a-community-nt

    Peanut84 (XP Pro) :
    Apache 2.0.54 // PHP 4.4.0 // MySQL 4.1.16-nt

  3. #3
    Junior Member
    Join Date
    May 2001
    Posts
    25
    Yes i want to get the full date in mm-dd-yyyy format for the week beginning monday

  4. #4
    Senior Member
    Join Date
    Sep 2002
    Location
    Toronto, ON
    Posts
    1,160
    There's no easy way to do this...you'll have to use some calculations. I think this is a way:

    Multiply the number of weeks by 7, to get a day in the specified week. Now subtract the weekday from that day. So for example, if the day was December 31, then it's tuesday, and you'd subtract 1 to get Monday's date.

    Of course, it's a bit more complicated than what I just explained, but what I said is just the general algorithm, you'll have to figure out the special cases.

    Diego

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,862
    The first ISO week of the year is the one with the first Thursday of the year in it. So let's see what day of the week January 1 of the $year was.

    PHP Code:
    $Jan1 mktime(1,1,1,1,1,$year);
    $weekdayJan1 date('w',$Jan1); 
    The exact time doesn't really matter - I just felt like specifying 01:01:01.

    Thursday is day number 4. So if January 1 was day $weekdayJan1, then the Thursday of the first week would be (11-$weekdayJan1)%7 days later (with the extra 7s there in case Jan 1 actually belonged to the last week of the previous year), and that first week started on the previous Monday (three days earlier than the Thursday, i.e., (11-$weekdayJan1)%7-3 days after Jan 1).
    A table that illustrates that expression:
    Code:
    $weekdayJan1: | Monday of first week of year is...
                  | (11-$weekdayJan1)%7-3
    --------------+-----------------------------------
    Monday        |  0 days later (Jan 1)
    Tuesday       | -1 days later (Dec 31)
    Wednesday     | -2 days later (Dec 30)
    Thursday      | -3 days later (Dec 29)
    Friday        |  3 days later (Jan 4)
    Saturday      |  2 days later (Jan 3)
    Sunday        |  1 days later (Jan 2).
    And code that uses it to get a timestamp for the Monday of the first week of the year:
    PHP Code:
    $FirstMonday strtotime(((11-$weekdayJan1)%7-3) . ' days'$Jan1); 
    Of course the first Monday of the current $week is $week-1 weeks later than that:
    PHP Code:
    $CurrentMonday strtotime(($week-1) . ' weeks'$FirstMonday); 
    $FirstMonday can be dropped and $CurrentMonday found with a single call strtotime(), passing it a string of the form "n weeks m days"; so an entire WeekToDate($week,$year) function can be written in three or four lines, viz:
    PHP Code:
    // Added _much_ later....
    function StartOfWeek($year$week)
    {
        
    $Jan1 mktime(1,1,1,1,1,$year);
        
    $MondayOffset = (11-date('w',$Jan1))%7-3;
        
    $desiredMonday strtotime(($week-1) . ' weeks '.$MondayOffset.' days'$Jan1);
        return 
    $desiredMonday;

    Tuesday, of course, is one day later, Wednesday is two days later, and so on. Sunday (according to ISO) is six days later, but you may reckon it's one day earlier. No, it doesn't format the result into any particular form. This function is not the place to be doing that sort of thing.
    Last edited by Weedpacket; 12-22-2006 at 06:40 AM. Reason: Added summary
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Junior Member
    Join Date
    May 2001
    Posts
    25
    Hi Everybody,

    Thank you all for your inputs.

    I have written the code as below. Hope this helps for the newcomers. Any suggestions/Modifications on the code is welcome.

    function WeekToDate ($week, $year)
    {
    $Jan1 = mktime (1, 1, 1, 1, 1, $year);
    $iYearFirstWeekNum = (int) strftime("%W",mktime (1, 1, 1, 1, 1, $year));

    if ($iYearFirstWeekNum == 1)
    {
    $week = $week - 1;
    }

    $weekdayJan1 = date ('w', $Jan1);
    $FirstMonday = strtotime(((4-$weekdayJan1)%7-3) . ' days', $Jan1);
    $CurrentMondayTS = strtotime(($week) . ' weeks', $FirstMonday);
    return ($CurrentMondayTS);
    }

    $iWeekNum = 1;
    $iYear = 2003;

    $sStartTS = WeekToDate ($iWeekNum, $iYear);
    $sStartDate = date ("F d, Y", $sStartTS);
    $sEndDate = date ("F d, Y", $sStartTS + (6*24*60*60));


    In the code i am taking into account that the first week number of the year maybe 0 OR 1 and then retrieving the timestamp from which I'll get the start and end date of the week.

    Thanks again guys,

    Bhavesh

  7. #7
    Junior Member
    Join Date
    Jan 2006
    Posts
    1

    solution to week starting date

    Seems like this isn't as complicated as people are making it:

    define('ONE_DAY', 60*60*24);
    $WeekDayNumber = date('w');
    $WeekBegin = date("Y-m-d",time() - ($WeekDayNumber - 1)*ONE_DAY);

    Hope I'm not misunderstanding the question.
    idbi
    Last edited by idbi; 01-10-2006 at 06:56 PM.

  8. #8
    Member
    Join Date
    Aug 2004
    Posts
    3,021
    idbi - I think he was talking about the week of the year, not the weekday number.

    Here's another entry:
    PHP Code:
    function week_start_date($wk_num$yr$first 1$format 'F d, Y')
    {
        
    $wk_ts  strtotime('+' $wk_num ' weeks'strtotime($yr '0101'));
        
    $mon_ts strtotime('-' date('w'$wk_ts) + $first ' days'$wk_ts);
        return 
    date($format$mon_ts);
    }

    $sStartDate week_start_date($week_number$year);
    $sEndDate   date('F d, Y'strtotime('+6 days'strtotime($sStartDate))); 
    Last edited by Installer; 01-11-2006 at 12:11 PM.

  9. #9
    Junior Member
    Join Date
    Jan 2006
    Posts
    2

    Question

    Hey all -

    I'm trying to use this function to get the correct date of what YYYY-mm-dd Monday was this week.

    Do I just use the last function posted by Installer? If so, what do I specify for week number?

    Could someone please post the "final" optimized code as an example? :-)

    Thanks,
    Peter.
    Last edited by webcan; 01-12-2006 at 10:42 AM.

  10. #10
    Junior Member
    Join Date
    Jan 2006
    Posts
    2
    OK, for anyone who wants another approach, I did this, it seems quite simple, and it works as well:

    PHP Code:
    if (date("w") == 0) {
        
    $adjuster 6;
    }
    else {
        
    $adjuster date("w") - 1;
    }
    $lowdate date("Y-m-d"strtotime("-" .$adjuster" days")); 
    The first if statement adjusts so that Sunday appears at the end of the week, not the beginning (which was needed for my purpose), but you could even optimize it to one line if you wanted to get rid of that, and just use date("w") - 1 in the strtotime statement?

    Peter.

  11. #11
    Senior Member
    Join Date
    Jul 2004
    Posts
    156
    I'm not printing out the results from the query. I'm checking to see if records exist. So, no, changing DESC to ASC won't work for what I want.

    The code checks to see if records exists for a week range and does not print out the results, but prints out:

    05 January 2006
    12 January 2006
    19 January 2006

    I would like it to print this out in reverse order:

    19 January 2006
    12 January 2006
    05 January 2006

    I'm reposting the code below and hope that someone can show me how to sort this:


    PHP Code:
     $week_start '2005-12-01';  
    $number_of_weeks 52;  
    make_weekly_queries($week_start$number_of_weeks);  
      
    function 
    make_weekly_queries($start$number) {  
      
             
            
    $now2 date('Y-m-d'); 
      
         
            for (
    $i 0$i $number$i++) {  
      
                    if (
    $i 0) {  
                        
    $start add_days(7$start);  
                    }  
      
                    if (
    $start $now2) { 
      
                    
    $query "SELECT * FROM MyTable WHERE" .  
                             
    " Expiration_Date BETWEEN '" $start .  
                             
    "' AND '" . ($end add_days(6$start)) ."' ORDER BY Expiration_Date DESC";  
                    
    $result mysql_query($query);  
      
                    if (
    mysql_num_rows($result) == 0) { 
      
                            echo 
    ''
      
                            } else { 
      
                            
    $st2 date('d F Y',strtotime($start)); 
                            
    $st3 date('Md',strtotime($start)); 
      
                            echo 
    '<a href="listings/'.$st3.'">'.$st2.'</a><br />'
                            while (
    $row mysql_fetch_assoc($result)) { 
                            }  
                    } 
            } 


      
    function 
    add_days($days$date) {  
      
            
    $date date('Y-m-d'strtotime('+' $days ' days'strtotime($date))); 
            return 
    $date;  
      

    "They told me this was Sri Lanka. I wanted a coconut toddy."

  12. #12
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,862
    Quote Originally Posted by webcan
    OK, for anyone who wants another approach, I did this, it seems quite simple, and it works as well:
    The original poster didn't have the exact date, only the week number and year. So for that code to be appropriate, you'd need a way to find the date given the week number and year. Which is the original problem.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  13. #13
    Junior Member
    Join Date
    May 2006
    Posts
    2
    Hi,

    I think the only acceptable solution was given by Weedpacket.
    All solutions which take the week number of January 1st as a base are definitely incorrect.

    I have written a function which give back the correct date when you provide the day, the year and the week number. Maybe it can be of use to someone.

    function datefromweeknr($aYear, $aWeek, $aDay)
    {
    $Days=array('xx','ma','di','wo','do','vr','za','zo');
    $DayOfWeek=array_search($aDay,$Days); //get day of week (1=Monday)
    $DayOfWeekRef = date("w", mktime (0,0,0,1,4,$aYear)); //get day of week of January 4 (always week 1)
    if ($DayOfWeekRef==0)
    $DayOfWeekRef=7;
    $ResultDate=mktime(0,0,0,1,4,$aYear)+((($aWeek-1)*7+($DayOfWeek-$DayOfWeekRef))*86400);
    return $ResultDate;
    };

  14. #14
    Junior Member Cartman's Avatar
    Join Date
    May 2006
    Posts
    2
    Here is very simple solution, passing a week no and returns the date.
    ISO standard state that, week 1 always fall on the week where in Jan 4 falls.
    Sample: get the day for the 4 week of the year

    $date = date('Y-m-d',strtotime("2006-01-04 + 4 weeks"));

  15. #15
    Junior Member
    Join Date
    May 2006
    Posts
    2
    Quote Originally Posted by Cartman
    Here is very simple solution, passing a week no and returns the date.
    ISO standard state that, week 1 always fall on the week where in Jan 4 falls.
    Sample: get the day for the 4 week of the year

    $date = date('Y-m-d',strtotime("2006-01-04 + 4 weeks"));


    That won't give you anything except the date x weeks after 2006-01-04


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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