Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Date to Day and Month (3 Letters)

Tags:

date

php

I have date of each post stored in date field in db. The format is DD/MM/YYYY (eg : 24/12/2013). Field is VARCHAR(50)

I want to print Day and month in a page (A Blog listing page). ie like "24 DEC" (Doesnt need year)

What's the best possible way to achieve it

Note : I'm using PHP

like image 886
Sharan Mohandas Avatar asked Nov 30 '14 12:11

Sharan Mohandas


2 Answers

Please start storing dates in Y-m-d format in DATE fields. Your life, and ours, would be much easier.

Until then, use this php solution:

$dt = DateTime::createFromFormat('!d/m/Y', '24/12/2013');
echo strtoupper($dt->format('j M')); # 24 DEC

demo

or mysql solution:

SELECT STR_TO_DATE('24/12/2013','%d/%m/%Y')
like image 180
Glavić Avatar answered Sep 18 '22 10:09

Glavić


Try this: You need to reverse the date i.e. Y/m/d format.

 $date = strtotime('2013/12/24');
 echo date('j M ', $date);

Output:

24 Dec

See in PHP Date Manual

like image 21
Riad Avatar answered Sep 20 '22 10:09

Riad