-1

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?

vrajesh
  • 2,890
  • 3
  • 22
  • 39
Jeebs
  • 3
  • 2

2 Answers2

2

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');

?>
Ekin
  • 1,929
  • 2
  • 30
  • 44
1

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');

?>
Samir Sheikh
  • 2,083
  • 2
  • 16
  • 33