Convert from MySQL datetime to another format with PHP

Convert from MySQL datetime to another format with PHP

Asked on December 28, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    This solution uses the format (mm/dd/yy H:M (AM/PM)) from the mysql:

    // $datetime is something like: 2014-01-31 13:05:59
    $time = strtotime($datetimeFromMysql);
    $myFormatForView = date("m/d/y g:i A", $time);
    // $myFormatForView is something like: 01/31/14 1:05 PM
    

    For more information see the PHP date formatting.

    Answered on December 28, 2018.
    Add Comment

    Using PHP 5:

    $oDate = new DateTime($row->createdate);
    $sDate = $oDate->format("Y-m-d H:i:s");
    
    Answered on December 28, 2018.
    Add Comment

    Try this single line code:

    $valid_date = date( 'm/d/y g:i A', strtotime($date));
    
    

    Also see this http://php.net/manual/en/function.date.php for more details.

    Answered on December 28, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.