-4

I have a function that prints out:

echo $post->EE_Event->primary_datetime()->start_date_and_time();

result:

21-02-15 08:00

But i want to display it as:

08:00 21st February, 2015

I need help with a function to grab the existing printed out date and reformat it.

I have tried a few snippets online, but nothing that worked.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Roy Barber
  • 172
  • 4
  • 16

3 Answers3

6

Try this code:

$yourDate = '21-02-15 08:00'; // $post->EE_Event->primary_datetime()->start_date_and_time()
$date = DateTime::createFromFormat('j-m-y G:i', $yourDate);
echo $date->format('G:i dS F, Y');

Try it online: http://ideone.com/a7EEK4

In PHP >= 5.3 we have excellent date API. See more here: http://php.net/manual/en/datetime.createfromformat.php

ajtamwojtek
  • 763
  • 6
  • 19
  • Works a treat thanks @jakon i found the 3rd line and was already trying to make that work, but it was the createfromformat that i hadnt tried. Thanks! – Roy Barber Dec 26 '14 at 18:08
1

PHP's DateTime class has exactly what you need. You want to use the createFromFormat to parse it in, then use format to print it back out.

$myDate = $post->EE_Event->primary_datetime()->start_date_and_time();
$myDateObj = DateTime::createFromFormat('d-m-y G:i', $myDate);

echo $myDateObj->format('G:i jS F, Y');
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
0
echo date("G:i dS F, Y", strtotime($post->EE_Event->primary_datetime()->start_date_and_time()));
Faruk Omar
  • 1,173
  • 2
  • 14
  • 22