Time/Date conversion year/week -> day/month/year

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jezternz
    New Member
    • Jan 2008
    • 145

    Time/Date conversion year/week -> day/month/year

    Okay. Simple as it sounds, but possibly complex.

    I have $week (which is the week in a year 1-52) and I have $year.
    I want to get the day of the saturday in the given $week and $year, so i want returned:
    $day (being day of month, will be the saturday of he origonal given week), $month (the month 0-12) and $year.

    Bit of a brain tease for me atleast. Any Idea's?
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Look under Calendar Calculating for a mathematical way of doing this.

    This one might be more useful ina code sense, but they have the same logic.

    I would really appreciate you sharing this code, as it is an interesting one to do! That second one looks like it will give more, but you might combine them for a solution.

    Comment

    • Jezternz
      New Member
      • Jan 2008
      • 145

      #3
      Originally posted by TheServant
      I would really appreciate you sharing this code, as it is an interesting one to do! That second one looks like it will give more, but you might combine them for a solution.
      sure thing, I will post with the working code, when I get it :)

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        Originally posted by Jezternz
        sure thing, I will post with the working code, when I get it :)
        Very interesting page (the second link). The doomsday code is amazing, and even possible to do in your head if your practice! Even if you get stuck, post the code, and maybe we can help then.

        Comment

        • Jezternz
          New Member
          • Jan 2008
          • 145

          #5
          im not sure if I explained what im trying to do properly, so in the chance I did not I will give an example.

          Input:
          we are in 2008, obviously.
          $year = 2008
          It is the 18th week of the year (this can be found by date('W');
          $week = 18

          now what I want for output is the day/month/year of the saturday of in this case week 18 so.
          output:
          again obviously 2008
          $year = 2008
          the saturday in the 18th week, is in May, however do not forget we want the date for the saturday, not the date for the 18th week, so although a week may start in one mont, it may be the next month im looking for,
          $month = 05
          The day of the saturday.
          $day = 3

          Thanks :)

          Comment

          • TheServant
            Recognized Expert Top Contributor
            • Feb 2008
            • 1168

            #6
            No one is going to make you this code. We fix your code, not make it. Because it is days of the week, and this changes from year to year, I gave you those links so that you could start developing a day finder code yourself. If it's not working, or you don't know how to do a part of it, that's when you post here.
            But you have to provide the start.
            I did understand what you wanted, but again, the coding involved is more than a few lines, so the unpaid coders (everyone) here will be unlikely to make it for you.

            Comment

            • Jezternz
              New Member
              • Jan 2008
              • 145

              #7
              ok will do, thanks again, ill report back when I got something.

              Cheerz, Josh

              Comment

              • coolsti
                Contributor
                • Mar 2008
                • 310

                #8
                I have done such similar manipulations for my application, and out of curiosity was trying to look for a code snippet that can solve this problem.

                But I am stuck with how to find out in PHP the month number when given the week number and year! Of course you can go the other way around, and find the week number for a given month and year using the date() function.

                Once you can find out how to get month from week number and year, the rest of what the OP is asking for is trivial, using some basic PHP functions.

                Does anyone know how to do this? Get the month from the week number plus year?

                Comment

                • Jezternz
                  New Member
                  • Jan 2008
                  • 145

                  #9
                  Yeh I know aye, Im pretty stumpted tbh, im sure it must be possible, I just dont know where to start and yeh I did wonder about it being easier to go back the other way.

                  Comment

                  • Jezternz
                    New Member
                    • Jan 2008
                    • 145

                    #10
                    Ok I got this:

                    Code:
                    // Define day + week in secs
                    $one_week_secs = 60*60*24*7;
                    $one_day_secs = 60*60*24;	
                    		
                    // Get week
                    $week = date('W'); // use current week number for an example
                    
                    // get upto start of year time
                    $upto_this_year_secs = mktime (0, 0, 0, 1, 0, date('Y'));
                    
                    // Get days till sat
                    $day_of_week = getdate($upto_this_year_secs);
                    [U]$days_till_sat = [B]6[/B]-$day_of_week['wday'];[/U]
                    
                    $timestamp = $upto_this_year_secs+($week*$one_week_secs)+($days_till_sat*$one_day_secs);
                    
                    $week_current_saturday = getdate($timestamp);
                    
                    echo "day:".$week_current_saturday['mday']." month:".$week_current_saturday['mon']." year:".$week_current_saturday['year']."<br/>";
                    Ok almost there :)
                    Only problem is, its saying this week's saturday is the 9th when it is actually the 10th. Now if I change the bolded/underlined 6 to '7' then the dates are corrected, however this doesnt make sence to me? and im not sure if I should change it as, although it works this year it might not work next year. I dont understand why 7 would work, you will have to think through the process, but because saturday is '6' (and sunday is '0') I dont get why it needs to be 7? any ideas?

                    Comment

                    • Jezternz
                      New Member
                      • Jan 2008
                      • 145

                      #11
                      Okay found the problem after massive amounts of debugging, it was daylight savings, gah, drives me crazy. anyway, to solve this I just added 12 hours of seconds onto the end (12*60*60)

                      cheerz

                      Comment

                      • ronverdonk
                        Recognized Expert Specialist
                        • Jul 2006
                        • 4259

                        #12
                        After some searching I found a VERY old (20 years?) C routine that I once wrote in the stone age. I managed to recode it to somehow acceptable PHP and the result is here.

                        P.S. It works for any week > 2 and < 54 and any year within the strtotime range. It also uses monday as the start of the week. But with some alteration it will do.[php]<?php
                        $monthtab=array (31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);

                        function showDayAndMonth ($dayno) {
                        global $monthtab;
                        $date_arr=array ();
                        for ($j=0; $j<12; $j++) {
                        if ($dayno <= $monthtab[$j]) {
                        $date_arr['day']=$dayno-$monthtab[$j-1];
                        $date_arr['mth']=++$j;
                        return $date_arr;
                        }
                        }
                        }

                        $year=2008;
                        $week=18;

                        $monday=0-(date("N", strtotime("1 January $year"))-2);
                        if ($year % 4 == 0)
                        for ($i=1; $i<12; $i++)
                        $monthtab[$i]++;
                        if ($week > 1 AND $week < 54) {
                        $week--; /* -1 voor offset */
                        $day_fr=($week * 7) + $monday; /* Dagnummer van weekstart */
                        $day_to=$day_fr +6; /* Dagnummer van weekeind */
                        $date_fr=showDa yAndMonth($day_ fr);
                        $date_to=showDa yAndMonth($day_ to);
                        }
                        else {
                        die ("Invalid week number");
                        }
                        echo "REQUESTED: week $week, year $year";
                        echo "<br>STARTD ATE: ".date("l F j, Y",strtotime($d ate_fr['day'].'-'.$date_fr['mth'].'-'.$year));
                        echo "<br>ENDDAT E: ".date("l F j, Y",strtotime($d ate_to['day'].'-'.$date_to['mth'].'-'.$year));
                        ?>[/php]I can explain the code if anyone is interested.

                        Ronald

                        Comment

                        • TheServant
                          Recognized Expert Top Contributor
                          • Feb 2008
                          • 1168

                          #13
                          Wow, that is really clever. Good job Ron!

                          Comment

                          • Jezternz
                            New Member
                            • Jan 2008
                            • 145

                            #14
                            Yeh that is impressive Ron, thanks heaps.
                            One thing I do wonder about though is wht would happen when february has 29 days not 28 :S.
                            $monthtab=array (31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
                            But regardless, this is far further then I got, thanks heaps.

                            Comment

                            • TheServant
                              Recognized Expert Top Contributor
                              • Feb 2008
                              • 1168

                              #15
                              Maybe just include an if statement with something like:

                              [PHP]if ($year == "is not a leap year") {
                              $monthtab=array (31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365);
                              } elseif ($year == "is a leap year") {
                              $monthtab=array (31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 336, 366);
                              }[/PHP]

                              You just nee to get a statement to define "is not a leap year". So probably something like divide the year by 4, make it a string, and search the string for a "." and if it is found, it is not a leap year. Then you also need to check if it is the 400th year, so if it can be divided by 400, if so it is also not a leap year.

                              Comment

                              Working...