0

I got a barebone joomla version (so just pure php using the joomla cms and table names). And the published date of an article is saved like this:

2016-04-06 14:38:52

How can I convert that string into something like:

6 April 2016

So ignore the time and only use the date in the correct format.

How can this be done using PHP (no joomla build in functions)?

twan
  • 2,450
  • 10
  • 32
  • 92

3 Answers3

2

You can do it with date and strtotime function of php

$t="2016-04-06 14:38:52";
echo date('j F Y', strtotime($t));

F :- A full textual representation of a month, such as January or March

j :- Day of the month without leading zeros

Y :- A full numeric representation of a year, 4 digits

OUTPUT

6 April 2016

Saty
  • 22,443
  • 7
  • 33
  • 51
1

You can do it also in object oriented way:

//create a DateTimeObject
$dateTime = new DateTime($date);

echo $dateTime->format('j F Y'); //output: 6 April 2016

Explanation of j F Y:

j - Day of the month without leading zeros
F - A full textual representation of a month, such as January or March
Y - A full numeric representation of a year, 4 digits
aslawin
  • 1,981
  • 16
  • 22
  • Please add more to your answer you basically copy pasted @Saty's answer – SuperDJ Apr 07 '16 at 09:12
  • It's not copy, @Saty has wrong order of letters in first argument of `date` function. – aslawin Apr 07 '16 at 09:14
  • The problem was that your answer didn't add anything new before your update. The OP could have adjusted the letter order after finding out what they represent – SuperDJ Apr 07 '16 at 09:16
  • @SuperDJ, because me and Saty added answers basically in same time. So I've edited my answer and shows this mechanism using `DateTime` object. Is that adding anything new now? – aslawin Apr 07 '16 at 09:24
  • 1
    Your solution is also correct !! – Saty Apr 07 '16 at 09:29
1

We can also do this using the DateTime class native to PHP.

$date = "2016-04-05 16:00:00";
$odate = DateTime:createFromFormat('Y-m-d H:i:s', $date);

Note that I use the createFromFormat method to initiate the DateTime object. This will prevent dates such as 2016-04-05 from being interpreted in the wrong way. This date could just as well be The 4th of May.

Now, to output

// Output
echo $oDate->format('j F Y');

F - A full textual representation of a month, such as January or March

j - Day of the month without leading zeros

Y - A full numeric representation of a year, 4 digits

Peter
  • 8,776
  • 6
  • 62
  • 95