Displays days and month only

Discussion in 'PHP' started by levani, Apr 19, 2009.

  1. #1
    When I get data from database the format is something like this:

    (2009-04-19 22:20:46)

    (Year-month-day hour-minute-second) but I want to display only month and days, not the whole date. Is the any php function for it.
    It's displayed using the variable $something->date

    Any suggestions?

    Thanks
     
    levani, Apr 19, 2009 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Again, explode is your friend here.
    Do something like this:
    
    <?php
    $date = "2009-04-19 20:34:19";
    $date = explode(" ",$date);
    $date = explode("-",$date[0]);
    echo $date[1]."-".$date[2];
    ?>
    
    PHP:
    (might have to change the code to fit your needs, but basically what this does is split the returned value from the database into an array, and then you chose which part of the array to show using [0] for the first item, [1] for the second and so forth.
     
    PoPSiCLe, Apr 19, 2009 IP
  3. levani

    levani Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    It work when I assign the variable $date to a simple text but I need it to assign another variable as I said above. This code doesn't seem to work:

    <?php
    $date = $something->date;
    $date = explode(" ",$date);
    $date = explode("-",$date[0]);
    echo $date[1]."-".$date[2];
    ?>
    PHP:
    I made it work!!! Thanks
     
    levani, Apr 19, 2009 IP
  4. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #4
    That is a bad implementation.

    You should go directly to your date function, so for $something->date you will find it in class something, function date.

    Then just change it from there directly, no need for the overhead.

    Peace,
     
    Barti1987, Apr 19, 2009 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $date_from_database = '2009-04-19 22:20:46';
    echo date('F j', strtotime($date_from_database));
    Code (markup):
     
    SmallPotatoes, Apr 19, 2009 IP