php 将日期转换为日和月(3 个字母)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27213359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Date to Day and Month (3 Letters)
提问by Sharan Mohandas
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)
我将每个帖子的日期存储date
在 db 的字段中。格式为 DD/MM/YYYY(例如:24/12/2013)。字段为 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
回答by Glavi?
Please start storing dates in Y-m-d format in DATE
fields. Your life, and ours, would be much easier.
请开始在DATE
字段中以 Ymd 格式存储日期。你和我们的生活会容易得多。
Until then, use this php solution:
在此之前,请使用此 php 解决方案:
$dt = DateTime::createFromFormat('!d/m/Y', '24/12/2013');
echo strtoupper($dt->format('j M')); # 24 DEC
or mysql solution:
或 mysql 解决方案:
SELECT STR_TO_DATE('24/12/2013','%d/%m/%Y')
回答by Riad
Try this: You need to reverse the date i.e. 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 日期手册
回答by Michael
that should do it:
应该这样做:
echo date_format($date,"d M");
回答by user1086500
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.
也许更好的方法是将 PHP 时间戳存储在 dB 日期字段中。然后,您还可以从一个字符串中提取小时、分钟和秒。
回答by kailash Chandra
$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'));