This question already has an answer here:
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)?
</div>
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
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
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