need help converting date string

Discussion in 'PHP' started by sensoryaddict, Sep 1, 2010.

  1. #1
    $date = "Aug 31 2010 9:21AM";

    Can someone kindly explain how to convert to

    $newdate = "2010-08-31"; // yyyy-mm-dd
     
    sensoryaddict, Sep 1, 2010 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Use string to time function:

    
    $date = "Aug 31 2010 9:21AM";
    $timestamp = strtotime($date);
    $newdate = date('Y-m-d',$timestamp);
    
    PHP:
     
    ThePHPMaster, Sep 1, 2010 IP
  3. sensoryaddict

    sensoryaddict Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3

    Fantastic! Worked like a charm.
     
    sensoryaddict, Sep 1, 2010 IP
  4. sensoryaddict

    sensoryaddict Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Just curious

    $date = "Aug 31 2010 9:21AM";
    $timestamp = strtotime($date);
    $newdate = date('Y-m-d',$timestamp);


    Lets say you wanted $newdate to be

    $newdate = 2010-08-30

    -1 day

    Is there quick way to do this?
     
    Last edited: Sep 2, 2010
    sensoryaddict, Sep 2, 2010 IP
  5. asdirc

    asdirc Active Member

    Messages:
    275
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    it is the same :)
     
    asdirc, Sep 2, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    The usage is self explanatory date($format, $timestamp); where $format is how its formatted - refer to the date function page for all the possible format characters.
     
    danx10, Sep 2, 2010 IP
  7. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #7
    Just subtract the day (in timestamp) from the timestamp.

    Every day is 24 hours * 60 minutes * 60 seconds = 86400

    
    $newdate = date('Y-m-d',$timestamp-86400);
    
    PHP:
     
    ThePHPMaster, Sep 2, 2010 IP