如何将此字符串格式化为日期[重复]

This question already has an answer here:

I have some strings that will be different dates and times. Each one will be different and I'm looking to format them to my liking.

For Example:

2016-04-15 14:20:00

I would like to be able to take this string and use PHP's Date function to format it to my liking.

For Example:

04/15 2:20pm

So would I take the string and convert it to a timestamp first? If so how do I go about doing that?

</div>

try this ..

<?php

    $time = strtotime('2016-04-15 14:20:00');
    echo date('m/d g:ia', $time);

?>

Second Way

<?php

$format = 'Y-m-d H:i:s';
$date = DateTime::createFromFormat($format, '2016-04-15 14:20:00');
echo  $date->format('m/d g:ia');

?>

You can return a new DateTime object formatted according to the specified format by using this function. Then you can format it as you like.

<?php

$testDate = '15-Feb-2009 11:00:34';

$date = DateTime::createFromFormat('j-M-Y H:i:s', $testDate);
echo $date->format('Y-m-d');

?>