将日期从YYYYMMDD转换为DD / MM / YYYY格式 [英] Convert date from YYYYMMDD to DD/MM/YYYY format in PHP
问题描述
我想以DD / MM / YYYY格式显示此日期。例如:26/12/2012
我想出的是使用 substr
来提取日,月和年份分开。
我想知道是否有更简单的方法来执行此操作。
另外,有没有办法将此日期转换为2012年12月26日格式不需要编写单独的代码?
您可以轻松使用DateTime类来执行
$ retrieval ='20121226';
$ date = DateTime :: createFromFormat('Ymd',$ retrieval);
echo $ date-> format('d / m / Y');
http://php.net/manual/en/datetime.format.php
I've a MySQL database table in which the date is stored in YYYYMMDD format. Eg: 20121226.
I want to display this date in DD/MM/YYYY format. Eg: 26/12/2012
What I came up with is to use substr
to extract the day, month and year separately.
I would like to know if there's an easier way to do this.
Also, is there a way to convert this date to "26 December 2012" format without the need to write separate code?
You can easily use the DateTime class to do this
$retrieved = '20121226';
$date = DateTime::createFromFormat('Ymd', $retrieved);
echo $date->format('d/m/Y');
http://php.net/manual/en/datetime.format.php
这篇关于将日期从YYYYMMDD转换为DD / MM / YYYY格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!