PhP question: Last month

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Naughty
    Confirmed User
    • Jul 2001
    • 6479

    #1

    PhP question: Last month

    I have to get the last month is simple 1,2,3 format, but I cannot get to it:/

    PHP Code:
    $lastmonth = date("n")-1; 
    
    This would work, however, NOT if you want to get the rusult 12, like this month the last months'number is 12, not 0.

    seks.ai for sale - ping me
  • notjoe
    Confirmed User
    • May 2002
    • 5599

    #2
    Originally posted by Naughty
    I have to get the last month is simple 1,2,3 format, but I cannot get to it:/

    PHP Code:
    $lastmonth = date("n")-1; 
    
    This would work, however, NOT if you want to get the rusult 12, like this month the last months'number is 12, not 0.

    $lastmonth = date("m") -1;
    if ($lastmonth == 0) {$lastmonth = 12;}


    There is a better way to do it using a mktime formula.

    http://www.php.net/date
    Last edited by notjoe; 01-18-2003, 04:49 AM.

    Comment

    • Bad B0y
      Confirmed User
      • Aug 2002
      • 160

      #3
      I have not tested this but I'd expect it to work.

      PHP Code:
      $lastmonth = (int)date("n") - 1;
      // and then notjoe's
      if ($lastmonth == 0) {$lastmonth = 12;} 
      
      Last edited by Bad B0y; 01-18-2003, 04:56 AM.

      Comment

      • Naughty
        Confirmed User
        • Jul 2001
        • 6479

        #4
        Originally posted by notjoe


        $lastmonth = date("m") -1;
        if ($lastmonth == 0) {$lastmonth = 12;}


        There is a better way to do it using a mktime formula.

        http://www.php.net/date
        I've been there for the past hours, but it returns the seconds format, I have tried many things before coming here to ask. The if-then formula crossed my mind too, but I wanted something else first.

        Thanks though.
        seks.ai for sale - ping me

        Comment

        • notjoe
          Confirmed User
          • May 2002
          • 5599

          #5
          Originally posted by Naughty


          I've been there for the past hours, but it returns the seconds format, I have tried many things before coming here to ask. The if-then formula crossed my mind too, but I wanted something else first.

          Thanks though.

          You could try #php on EFNet (irc), or wait a few hours till i smoke another joint and have a coffee.. once i wake up i'll be able to process php code better ;)

          Comment

          • Naughty
            Confirmed User
            • Jul 2001
            • 6479

            #6
            Originally posted by Bad B0y
            I have not tested this but I'd expect it to work.

            PHP Code:
            $lastmonth = (int)date("n") - 1; 
            
            This obviously did not work, as you gave the same solution as we already had.

            No Joe's version works well, but it is not what I had in mind

            I'll go with that though.
            seks.ai for sale - ping me

            Comment

            • Bad B0y
              Confirmed User
              • Aug 2002
              • 160

              #7
              Forget my previous post. Here you go:

              $lastmonth = date ("n",mktime (0,0,0,date("m")-1,date("d"), date("Y")));

              and again I did not test this.

              Comment

              • Naughty
                Confirmed User
                • Jul 2001
                • 6479

                #8
                Originally posted by Bad B0y
                Forget my previous post. Here you go:

                $lastmonth = date ("n",mktime (0,0,0,date("m")-1,date("d"), date("Y")));

                and again I did not test this.
                I already tested that hours ago, didn't work.
                seks.ai for sale - ping me

                Comment

                • Naughty
                  Confirmed User
                  • Jul 2001
                  • 6479

                  #9
                  Originally posted by Naughty


                  I already tested that hours ago, didn't work.
                  hmm, I must have screwed it up a little when I tried it, it seems to work now
                  seks.ai for sale - ping me

                  Comment

                  • fletcher
                    Confirmed User
                    • Jan 2003
                    • 698

                    #10
                    In case you're still having issues:

                    PHP Code:
                    $lastMonth = (((int)date("m") - 1) > 0) ? (int)date("m") - 1 : 12; 
                    
                     
                    [email protected]
                    ICQ: 6411138

                    Comment

                    • Naughty
                      Confirmed User
                      • Jul 2001
                      • 6479

                      #11
                      Originally posted by fletcher
                      In case you're still having issues:

                      PHP Code:
                      $lastMonth = (((int)date("m") - 1) > 0) ? (int)date("m") - 1 : 12; 
                      
                      I have issues alright .. however, not with this problem anymore
                      seks.ai for sale - ping me

                      Comment

                      • fletcher
                        Confirmed User
                        • Jan 2003
                        • 698

                        #12
                        If you have any questions, hit me up at 225006418 on ICQ and I can most likely give you a hand.
                         
                        [email protected]
                        ICQ: 6411138

                        Comment

                        Working...