简体   繁体   English

如何将日期和时间字符串转换为不同的格式?

[英]How to Convert Date and Time String into Different format?

I am saving date and time in a single column ie 2015-04-15 13:22:58 and I want to show this time and date in this format: 15th April 2015, 1:22 pm. 我将日期和时间保存在一个列中,即2015-04-15 13:22:58,我想以以下格式显示此时间和日期:2015年4月15日,下午1:22。 I have used this code: 我使用了以下代码:

$dt = '2015-04-15 13:22:58';
echo date("jS F Y", strtotime($dt)); 

and it shows only date (15th April 2015). 并且仅显示日期(2015年4月15日)。 How can I fetch time along with date?? 如何获取日期和时间? Any help would be highly appreciable. 任何帮助都是非常可观的。

change "jS FY" to "js FY g:ia" . "jS FY"更改为"js FY g:ia" That will output the time in your desired format as well as the date. 这将以您想要的格式输出时间以及日期。

$dt = '2015-04-15 13:22:58';
echo date("jS F Y g:i a", strtotime($dt));

Output: 输出:

15th April 2015 1:22 pm

Try 尝试

$dt = '2015-04-15 13:22:58';

echo date("jS F Y h:i a", strtotime($dt)); 
//Result is 15th April 2015 01:22 pm

echo date("jS F Y g:i a", strtotime($dt)); 
// Result is 15th April 2015 1:22 pm

如果要用php格式化日期时间,则可以使用date_format这样。

echo date_format(strtotime($dt), "jS F Y g:i A");

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

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