Explode Function on Date

Discussion in 'PHP' started by grant.r.brown, Jul 28, 2011.

  1. #1
    Hi,

    I'm currently using the follow code to explode a date:

    PHP Code:

    list($y, $m, $d) = explode('-', substr($postted_date, 0, 9));

    The postted_date looks like this:

    2011-07-26 17:18:20

    Its definitely breaking up the date to list the year, month and day, but for example with the above date for month its only displaying the '7' and I need it to display the month as '07'. Same case with the day.

    Suggestions?

    Thanks
     
    grant.r.brown, Jul 28, 2011 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    just do it like this.

    
    $y = date('Y',strtotime('2011-07-26 17:18:20'));
    $m = date('m',strtotime('2011-07-26 17:18:20'));
    $d = date('d',strtotime('2011-07-26 17:18:20'));
    
    PHP:
     
    stephan2307, Jul 28, 2011 IP
  3. grant.r.brown

    grant.r.brown Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I finally figured it out like this:

    list($y, $m, $d) = explode('-', substr($postted_date, 0, 10));

    I was supposed to be grabbing the first 10 chars of the $postted_date, not 9.
     
    grant.r.brown, Jul 28, 2011 IP