PHP convert date format dd/mm/yyyy => yyyy-mm-dd [duplicate]

Dates in the m/d/y or d-m-y formats are disambiguated by looking
at the separator between the various components: if the separator is a
slash (/), then the American m/d/y is assumed; whereas if the
separator is a dash (-) or a dot (.), then the European d-m-y
format is assumed. Check more here.

Use the default date function.

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

EDIT I just tested it, and somehow, PHP doesn’t work well with dd/mm/yyyy format. Here’s another solution.

$var="20/04/2012";
$date = str_replace("https://stackoverflow.com/", '-', $var);
echo date('Y-m-d', strtotime($date));

Leave a Comment