Week number unique sortable integer id












0















My question is about programming philosophy, I give an example in PHP language but can be asked in any programming language:




  • If I have to give an unique sortable integer ID to a day, I can use 'Ymd' format

  • If I have to give an unique sortable integer ID to a month, I can use 'Ym' format

  • If I have to give an unique sortable integer ID to a week, I cannot use 'YW' format


Because of 2017-01-01 and 2017-12-31 will return the same week ID:



$date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
$date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
echo $date1->format('YW').' '.$date2->format('YW');
// returns 201752 201752


So I can use the first day ID of the week as unique sortable integer ID week but there is maybe a simpler way to solve it ? A better practice ?










share|improve this question



























    0















    My question is about programming philosophy, I give an example in PHP language but can be asked in any programming language:




    • If I have to give an unique sortable integer ID to a day, I can use 'Ymd' format

    • If I have to give an unique sortable integer ID to a month, I can use 'Ym' format

    • If I have to give an unique sortable integer ID to a week, I cannot use 'YW' format


    Because of 2017-01-01 and 2017-12-31 will return the same week ID:



    $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
    $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
    echo $date1->format('YW').' '.$date2->format('YW');
    // returns 201752 201752


    So I can use the first day ID of the week as unique sortable integer ID week but there is maybe a simpler way to solve it ? A better practice ?










    share|improve this question

























      0












      0








      0








      My question is about programming philosophy, I give an example in PHP language but can be asked in any programming language:




      • If I have to give an unique sortable integer ID to a day, I can use 'Ymd' format

      • If I have to give an unique sortable integer ID to a month, I can use 'Ym' format

      • If I have to give an unique sortable integer ID to a week, I cannot use 'YW' format


      Because of 2017-01-01 and 2017-12-31 will return the same week ID:



      $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
      $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
      echo $date1->format('YW').' '.$date2->format('YW');
      // returns 201752 201752


      So I can use the first day ID of the week as unique sortable integer ID week but there is maybe a simpler way to solve it ? A better practice ?










      share|improve this question














      My question is about programming philosophy, I give an example in PHP language but can be asked in any programming language:




      • If I have to give an unique sortable integer ID to a day, I can use 'Ymd' format

      • If I have to give an unique sortable integer ID to a month, I can use 'Ym' format

      • If I have to give an unique sortable integer ID to a week, I cannot use 'YW' format


      Because of 2017-01-01 and 2017-12-31 will return the same week ID:



      $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
      $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
      echo $date1->format('YW').' '.$date2->format('YW');
      // returns 201752 201752


      So I can use the first day ID of the week as unique sortable integer ID week but there is maybe a simpler way to solve it ? A better practice ?







      php datetime






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 27 '18 at 15:34









      alexalex

      3,23152148




      3,23152148
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use o instead:



          echo $date1->format('oW').' '.$date2->format('oW');
          //201652 201752



          o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)




          http://php.net/manual/en/function.date.php






          share|improve this answer
























          • Exactly what I need, thank you very much for your help

            – alex
            Nov 27 '18 at 15:42











          • No problem @alex. You can mark as accepted if it works for you.

            – Felippe Duarte
            Nov 27 '18 at 15:44



















          1














          You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)



          Reason:



          Y is year from the date



          o is ISO-8601 year number



          W is ISO-8601 week number of year



          $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
          $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
          echo $date1->format('YW').' '.$date2->format('YW');
          echo PHP_EOL;
          echo $date1->format('oW').' '.$date2->format('oW');


          DEMO: https://3v4l.org/sMKAF






          share|improve this answer


























          • Thank you very much, I was not aware about 'o' format.

            – alex
            Nov 27 '18 at 15:48











          • You're welcome, Best of luck mate:)

            – Always Sunny
            Nov 27 '18 at 15:49











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503048%2fweek-number-unique-sortable-integer-id%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You can use o instead:



          echo $date1->format('oW').' '.$date2->format('oW');
          //201652 201752



          o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)




          http://php.net/manual/en/function.date.php






          share|improve this answer
























          • Exactly what I need, thank you very much for your help

            – alex
            Nov 27 '18 at 15:42











          • No problem @alex. You can mark as accepted if it works for you.

            – Felippe Duarte
            Nov 27 '18 at 15:44
















          1














          You can use o instead:



          echo $date1->format('oW').' '.$date2->format('oW');
          //201652 201752



          o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)




          http://php.net/manual/en/function.date.php






          share|improve this answer
























          • Exactly what I need, thank you very much for your help

            – alex
            Nov 27 '18 at 15:42











          • No problem @alex. You can mark as accepted if it works for you.

            – Felippe Duarte
            Nov 27 '18 at 15:44














          1












          1








          1







          You can use o instead:



          echo $date1->format('oW').' '.$date2->format('oW');
          //201652 201752



          o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)




          http://php.net/manual/en/function.date.php






          share|improve this answer













          You can use o instead:



          echo $date1->format('oW').' '.$date2->format('oW');
          //201652 201752



          o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)




          http://php.net/manual/en/function.date.php







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '18 at 15:40









          Felippe DuarteFelippe Duarte

          10.6k21624




          10.6k21624













          • Exactly what I need, thank you very much for your help

            – alex
            Nov 27 '18 at 15:42











          • No problem @alex. You can mark as accepted if it works for you.

            – Felippe Duarte
            Nov 27 '18 at 15:44



















          • Exactly what I need, thank you very much for your help

            – alex
            Nov 27 '18 at 15:42











          • No problem @alex. You can mark as accepted if it works for you.

            – Felippe Duarte
            Nov 27 '18 at 15:44

















          Exactly what I need, thank you very much for your help

          – alex
          Nov 27 '18 at 15:42





          Exactly what I need, thank you very much for your help

          – alex
          Nov 27 '18 at 15:42













          No problem @alex. You can mark as accepted if it works for you.

          – Felippe Duarte
          Nov 27 '18 at 15:44





          No problem @alex. You can mark as accepted if it works for you.

          – Felippe Duarte
          Nov 27 '18 at 15:44













          1














          You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)



          Reason:



          Y is year from the date



          o is ISO-8601 year number



          W is ISO-8601 week number of year



          $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
          $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
          echo $date1->format('YW').' '.$date2->format('YW');
          echo PHP_EOL;
          echo $date1->format('oW').' '.$date2->format('oW');


          DEMO: https://3v4l.org/sMKAF






          share|improve this answer


























          • Thank you very much, I was not aware about 'o' format.

            – alex
            Nov 27 '18 at 15:48











          • You're welcome, Best of luck mate:)

            – Always Sunny
            Nov 27 '18 at 15:49
















          1














          You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)



          Reason:



          Y is year from the date



          o is ISO-8601 year number



          W is ISO-8601 week number of year



          $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
          $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
          echo $date1->format('YW').' '.$date2->format('YW');
          echo PHP_EOL;
          echo $date1->format('oW').' '.$date2->format('oW');


          DEMO: https://3v4l.org/sMKAF






          share|improve this answer


























          • Thank you very much, I was not aware about 'o' format.

            – alex
            Nov 27 '18 at 15:48











          • You're welcome, Best of luck mate:)

            – Always Sunny
            Nov 27 '18 at 15:49














          1












          1








          1







          You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)



          Reason:



          Y is year from the date



          o is ISO-8601 year number



          W is ISO-8601 week number of year



          $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
          $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
          echo $date1->format('YW').' '.$date2->format('YW');
          echo PHP_EOL;
          echo $date1->format('oW').' '.$date2->format('oW');


          DEMO: https://3v4l.org/sMKAF






          share|improve this answer















          You've to be aware when using week numbers with years. There is already a contribution note at php.net for this scenario 6 years back. Have a look here, Hope this will help you understand clearly :)



          Reason:



          Y is year from the date



          o is ISO-8601 year number



          W is ISO-8601 week number of year



          $date1 = DateTime::createFromFormat('Y-m-d', '2017-01-01');
          $date2 = DateTime::createFromFormat('Y-m-d', '2017-12-31');
          echo $date1->format('YW').' '.$date2->format('YW');
          echo PHP_EOL;
          echo $date1->format('oW').' '.$date2->format('oW');


          DEMO: https://3v4l.org/sMKAF







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 27 '18 at 15:50

























          answered Nov 27 '18 at 15:44









          Always SunnyAlways Sunny

          16.4k32848




          16.4k32848













          • Thank you very much, I was not aware about 'o' format.

            – alex
            Nov 27 '18 at 15:48











          • You're welcome, Best of luck mate:)

            – Always Sunny
            Nov 27 '18 at 15:49



















          • Thank you very much, I was not aware about 'o' format.

            – alex
            Nov 27 '18 at 15:48











          • You're welcome, Best of luck mate:)

            – Always Sunny
            Nov 27 '18 at 15:49

















          Thank you very much, I was not aware about 'o' format.

          – alex
          Nov 27 '18 at 15:48





          Thank you very much, I was not aware about 'o' format.

          – alex
          Nov 27 '18 at 15:48













          You're welcome, Best of luck mate:)

          – Always Sunny
          Nov 27 '18 at 15:49





          You're welcome, Best of luck mate:)

          – Always Sunny
          Nov 27 '18 at 15:49


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53503048%2fweek-number-unique-sortable-integer-id%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Contact image not getting when fetch all contact list from iPhone by CNContact

          count number of partitions of a set with n elements into k subsets

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks