Search code examples
phpmysqldateoutputvarchar

Extract Information From Date (varchar) and transmute into output


Background

I am extending a web application which must 'extract' information from a varchar variable in a mySQL table. Now although I am quite familiar the is the most convoluted way to actually store the date in the column, I cannot change the table structure currently, or it will 'break' the rest of the system. So with this information given, how can I output (using php) this field as so:

Column Data

2013-05-12 13:18:14

Output Data

$html .= "<td>" . $row['created_on'] . "</td>";

should print:

Instance1: 5/12/13
Instance2: May 12th, 2013
Instance3: 5/12/13, 1:18 PM

Solution

  • You can do it like this:
    First turn it into its timestamp value by strtotime(), then you can show it in many ways. Follow this link: date()

    $col = '2013-05-12 13:18:14';
    $timestamp = strtotime($col);
    
    $instance1 = date('m/d/y',$timestamp);
    $instance2 = date('M jS Y',$timestamp);
    $instance3 = date('m/d/y , h:i:s A',$timestamp);
    
    echo $instance1.'<br>';
    echo $instance2.'<br>';
    echo $instance3;