简体   繁体   English

将日期转换为日和月(3 个字母)

[英]Convert Date to Day and Month (3 Letters)

I have date of each post stored in date field in db.我将每个帖子的date存储在 db 的date字段中。 The format is DD/MM/YYYY (eg : 24/12/2013).格式为 DD/MM/YYYY(例如:24/12/2013)。 Field is VARCHAR(50)字段为 VARCHAR(50)

I want to print Day and month in a page (A Blog listing page).我想在页面(博客列表页面)中打印日期和月份。 ie like "24 DEC" (Doesnt need year)即像“24 DEC”(不需要年份)

What's the best possible way to achieve it实现它的最佳方法是什么

Note : I'm using PHP注意:我正在使用 PHP

Please start storing dates in Ymd format in DATE fields.请开始在DATE字段中以 Ymd 格式存储日期。 Your life, and ours, would be much easier.你和我们的生活会容易得多。

Until then, use this php solution:在此之前,请使用此 php 解决方案:

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

demo演示

or mysql solution:或 mysql 解决方案:

SELECT STR_TO_DATE('24/12/2013','%d/%m/%Y')

Try this: You need to reverse the date ie Y/m/d format.试试这个:您需要反转日期,即 Y/m/d 格式。

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

Output:输出:

24 Dec

See in PHP Date Manual请参阅PHP 日期手册

应该这样做:

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.也许更好的方法是将 PHP 时间戳存储在 dB 日期字段中。 Then you can in addition extract hours, minutes and seconds from one string.然后,您还可以从一个字符串中提取小时、分钟和秒。

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

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

simply do this简单地做这个

echo date('M j,Y');

it will give result它会给出结果

Jun 3,2020

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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