简体   繁体   中英

Convert Date to Day and Month (3 Letters)

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

Please start storing dates in Ymd 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')

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

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

Output:

24 Dec

See in PHP Date Manual

应该这样做:

echo date_format($date,"d M");

You could split the string using

$dateArray = explode("/", $date);

and you'll have

$year = $dateArray[2];
$month = $dateArray[1];
$day = $dateArray[0];

Maybe a better way is to store PHP timestamp in the dB datefield. Then you can in addition extract hours, minutes and seconds from one string.

$dt = DateTime::createFromFormat('!d/m/Y', '18/10/2016');

echo strtoupper($dt->format('d M'));

simply do this

echo date('M j,Y');

it will give result

Jun 3,2020

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM