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
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;
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.