MySQL Timestamp into PHP Format

Discussion in 'PHP' started by cancer10, Mar 16, 2008.

  1. #1
    I have a timestamp column in a mysql table.

    Using PHP, how do I convert the timestamp format which is "YYYY-MM-DD HH:MM:SS" into a format like "March 16 2008, 08:41 am" ?


    Please help



    Thanx
     
    cancer10, Mar 16, 2008 IP
  2. mytshans

    mytshans Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    maybe you can try date('F d Y, h:i a',$yourtimestamp)
     
    mytshans, Mar 16, 2008 IP
  3. dhana_space1

    dhana_space1 Peon

    Messages:
    213
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    check might be helpful!
    <?php

    $date = $row->timestamp; //get the value of the timestamp fied from the table
    $format="%B %d %Y, %H:%M %p";

    if ( $date && ereg( "([0-9]{4})-([0-9]{2})-([0-9]{2})[ ]([0-9]{2}):([0-9]{2}):([0-9]{2})", $date, $regs ) ) {
    $date = mktime( $regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1] );
    $date = $date > -1 ? strftime( $format, $date + ($offset*60*60) ) : '-';
    }
    echo $date;
     
    dhana_space1, Mar 17, 2008 IP
  4. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #4
    If solving your problem (which is to get time in specified format) and not answering your question (how to do this using php) I can suggest a little change of your sql query to something like this:
    
    select date_format(date_field,'%M %d %Y, %h:%i %r') as new_date from
    
    Code (markup):
    More mysql options are listed here
    But using this query you will get "AM" or "PM" in capital letters so you can fetch timestamp in unix format with this query:
    
    select UNIX_TIMESTAMP(date_field) as new_date from
    
    Code (markup):
    and after that use
    
    date('F d Y, h:i a',$row['new_date'])
    
    PHP:
    as mytshans said.
     
    AsHinE, Mar 17, 2008 IP