This question already has an answer here:
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.
</div>
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
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');
echo date("G:i dS F, Y", strtotime($post->EE_Event->primary_datetime()->start_date_and_time()));