Calculate last month, last 3 months, and last 6 months from today












6














I am looking for some ideas on how this could be improved:



switch ($periodValue) {
case "lastmonth":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
break;
case "last3month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
break;
case "last6month":
$until = mktime(0, 0, 0, date('n'), 1, date('Y'));
$from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
break;


How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?










share|improve this question





























    6














    I am looking for some ideas on how this could be improved:



    switch ($periodValue) {
    case "lastmonth":
    $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
    $from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
    break;
    case "last3month":
    $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
    $from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
    break;
    case "last6month":
    $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
    $from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
    break;


    How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?










    share|improve this question



























      6












      6








      6


      2





      I am looking for some ideas on how this could be improved:



      switch ($periodValue) {
      case "lastmonth":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
      break;
      case "last3month":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
      break;
      case "last6month":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
      break;


      How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?










      share|improve this question















      I am looking for some ideas on how this could be improved:



      switch ($periodValue) {
      case "lastmonth":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until) - 1, 1, date('Y', $until));
      break;
      case "last3month":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until)-3, 1, date('Y', $until));
      break;
      case "last6month":
      $until = mktime(0, 0, 0, date('n'), 1, date('Y'));
      $from = mktime(0, 0, 0, date('n', $until)-6, 1, date('Y', $until));
      break;


      How could I make this "smarter", so that it works out if we are near the end of the month it will show the last 30 days instead of the previous month?







      php datetime






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 10 '14 at 14:12









      Jamal

      30.3k11116226




      30.3k11116226










      asked Sep 25 '12 at 18:50









      John Magnolia

      158116




      158116






















          2 Answers
          2






          active

          oldest

          votes


















          8














          Try the DateTime class. In combination with the DateInterval class you can do something like this:



          $until = new DateTime();
          $interval = new DateInterval('P2M');//2 months
          $from = $until->sub($interval);
          echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');


          Also, you have to set the $until variable only once, before the switch statement.






          share|improve this answer































            0














            The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()?



            $period = 30;//90 for 3 months, 180 for 6 months

            $until = strtotime( 'now' );
            $from = strtotime( "-$period days" );
            //or
            $period = 'last month';//'last 3 months', or 'last 6 months'
            $from = strtotime( $period );


            I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime() is a pretty smart function, so who knows. Hope it helps.






            share|improve this answer





















            • 90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
              – deathemperor
              May 31 '18 at 9:55











            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            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: "196"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fcodereview.stackexchange.com%2fquestions%2f15923%2fcalculate-last-month-last-3-months-and-last-6-months-from-today%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









            8














            Try the DateTime class. In combination with the DateInterval class you can do something like this:



            $until = new DateTime();
            $interval = new DateInterval('P2M');//2 months
            $from = $until->sub($interval);
            echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');


            Also, you have to set the $until variable only once, before the switch statement.






            share|improve this answer




























              8














              Try the DateTime class. In combination with the DateInterval class you can do something like this:



              $until = new DateTime();
              $interval = new DateInterval('P2M');//2 months
              $from = $until->sub($interval);
              echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');


              Also, you have to set the $until variable only once, before the switch statement.






              share|improve this answer


























                8












                8








                8






                Try the DateTime class. In combination with the DateInterval class you can do something like this:



                $until = new DateTime();
                $interval = new DateInterval('P2M');//2 months
                $from = $until->sub($interval);
                echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');


                Also, you have to set the $until variable only once, before the switch statement.






                share|improve this answer














                Try the DateTime class. In combination with the DateInterval class you can do something like this:



                $until = new DateTime();
                $interval = new DateInterval('P2M');//2 months
                $from = $until->sub($interval);
                echo 'from' . $from->format('Y-m-d') . 'until' . $until->format('Y-m-d');


                Also, you have to set the $until variable only once, before the switch statement.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 10 '14 at 9:17









                Anil Jaiswal

                33




                33










                answered Sep 25 '12 at 20:59









                Dirk McQuickly

                20114




                20114

























                    0














                    The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()?



                    $period = 30;//90 for 3 months, 180 for 6 months

                    $until = strtotime( 'now' );
                    $from = strtotime( "-$period days" );
                    //or
                    $period = 'last month';//'last 3 months', or 'last 6 months'
                    $from = strtotime( $period );


                    I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime() is a pretty smart function, so who knows. Hope it helps.






                    share|improve this answer





















                    • 90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                      – deathemperor
                      May 31 '18 at 9:55
















                    0














                    The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()?



                    $period = 30;//90 for 3 months, 180 for 6 months

                    $until = strtotime( 'now' );
                    $from = strtotime( "-$period days" );
                    //or
                    $period = 'last month';//'last 3 months', or 'last 6 months'
                    $from = strtotime( $period );


                    I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime() is a pretty smart function, so who knows. Hope it helps.






                    share|improve this answer





















                    • 90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                      – deathemperor
                      May 31 '18 at 9:55














                    0












                    0








                    0






                    The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()?



                    $period = 30;//90 for 3 months, 180 for 6 months

                    $until = strtotime( 'now' );
                    $from = strtotime( "-$period days" );
                    //or
                    $period = 'last month';//'last 3 months', or 'last 6 months'
                    $from = strtotime( $period );


                    I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime() is a pretty smart function, so who knows. Hope it helps.






                    share|improve this answer












                    The date/time functions in PHP are a tricky thing. Last I looked into this there were known bugs with many of the date/time functions, especially around leap years, but no solutions. I don't know if that has changed, haven't had the need to look, so this answer might not be the best one, but have you tried strtotime()?



                    $period = 30;//90 for 3 months, 180 for 6 months

                    $until = strtotime( 'now' );
                    $from = strtotime( "-$period days" );
                    //or
                    $period = 'last month';//'last 3 months', or 'last 6 months'
                    $from = strtotime( $period );


                    I've not tried any of the above, but I'm fairly confident the first half should work, but I'm not sure about the last half. However, strtotime() is a pretty smart function, so who knows. Hope it helps.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 25 '12 at 20:19









                    mseancole

                    5,8781926




                    5,8781926












                    • 90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                      – deathemperor
                      May 31 '18 at 9:55


















                    • 90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                      – deathemperor
                      May 31 '18 at 9:55
















                    90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                    – deathemperor
                    May 31 '18 at 9:55




                    90 days ago isn't actually 3 months ago. for example if now is 31/5/2018 which is the time of this comment.
                    – deathemperor
                    May 31 '18 at 9:55


















                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • 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.


                    Use MathJax to format equations. MathJax reference.


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





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • 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%2fcodereview.stackexchange.com%2fquestions%2f15923%2fcalculate-last-month-last-3-months-and-last-6-months-from-today%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

                    Terni

                    A new problem with tex4ht and tikz

                    Sun Ra