Problem with date and getting last month

Discussion in 'PHP' started by Fruktkaka, Oct 31, 2011.

  1. #1
    I've got a really weird problem that perhaps is more server related than PHP related, but well, I don't know.

    If I do the following in PHP:

    date("M")
    Code (markup):
    I get: Oct
    Which is correct.

    If I try to get the date from the previous month by using:

    date("M",strtotime("-1 month"))
    Code (markup):
    I get: Oct
    Which is not correct as I would like it to say Sep.

    date("Y-m-d")
    date("Y-m-d",strtotime("-1 month"))
    date("Y-m-d", mktime(0, 0, 0, date("m")-1, date("d"), date("Y")))
    
    Code (markup):
    Gives me:
    2011-10-31
    2011-10-01
    2011-10-01

    Can anyone figure out what's going on?

    (Date and hwclock on my server gives the same time as date();)
     
    Fruktkaka, Oct 31, 2011 IP
  2. Invisible_Flash

    Invisible_Flash Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    strtotime("-1 month")
    it's decreasing 30 days in spite of 31 days which is creating the problem.
     
    Invisible_Flash, Oct 31, 2011 IP
  3. Fruktkaka

    Fruktkaka Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #3
    But why does it do that? date("m")-1 does the same, which I find strange?
     
    Fruktkaka, Oct 31, 2011 IP