1

I want to convert date format in php.

I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.

I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.

How do I do that?

2 Answers2

2

Use createFromFormat function

$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
splash58
  • 26,043
  • 3
  • 22
  • 34
0

As i Commented out both the format, You need to change the format before changing final for final format.

If you need 3 at 03, just use g at h.

General: Online Check

$date = '27-05-2016 15:30';

echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016

Or ,

using Object Oriented: Online Check

$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);

echo $date->format('h:i A d-M-Y');
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42