PHP convert date format dd/mm/yyyy => yyyy-mm-dd

PHP convert date format dd/mm/yyyy => yyyy-mm-dd

Asked on December 25, 2018 in PHP.
Add Comment


  • 3 Answer(s)

    Have a look at he solution below,

    Basically, in the format of m/d/y or d-m-y  date are rephrase by looking at the separator between the many components. The American m/d/y is assumed incase the separator is a slash that means /, ; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. For additional information.

    Here is an example for use the default date function in code.

    $var = "20/04/2012";
    echo date("Y-m-d", strtotime($var) );
    

    If suppose PHP does not work with dd/mm/yyyy format well use the below solution,

    $var = '20/04/2012';
    $date = str_replace('/', '-', $var);
    echo date('Y-m-d', strtotime($date));
    
    Answered on December 25, 2018.
    Add Comment

    A function in php which is used to convert the date, month, year format named DateTime::createFromFormat

    $date = DateTime::createFromFormat('d/m/Y', "24/04/2012");
    echo $date->format('Y-m-d');
    

    It returns,

    2012-04-24
    
    Answered on December 25, 2018.
    Add Comment

    By using this solution one can easily convert the date format without using date() function,

    $var = '20/04/2012';
    echo implode("-", array_reverse(explode("/", $var)));
    
    Answered on December 25, 2018.
    Add Comment


  • Your Answer

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