change date format from Y-m-d to d-m-Y [duplicate]

2019-09-21 08:27发布

问题:

This question already has an answer here:

  • Convert one date format into another in PHP 15 answers

based on this code :

$tarikh = mysql_real_escape_string($_POST['tarikh']);
    $tarikh = date('Y-m-d', strtotime($_POST['tarikh']));

can someone show me to convert the format from Y-m-d to d-m-Y format ? thanks

回答1:

try this

$tarikh = mysql_real_escape_string($_POST['tarikh']);
$myDateTime = DateTime::createFromFormat('Y-m-d', $tarikh);
$newDateString = $myDateTime->format('d-m-Y');