如何将日期/时间从24小时格式转换为12小时上午/下午? [英] How do I convert date/time from 24-hour format to 12-hour AM/PM?
问题描述
从数据Feed中我以这种格式获取日期和时间:
From a data feed I'm getting date and time in this format:
19:24:15 06/13/2013
我需要将其转换为12小时上午/下午没有秒。所以上面的时间是下午7:24。日期应保持在mm / dd / yyyy格式。有没有一个优雅的方式来做这个在PHP(而不是MySQL)?谢谢!
I need to have it converted to be in 12-hour AM/PM without the seconds. So the above time would be 7:24 PM. The date should remain in mm/dd/yyyy format. Is there an elegant way to do this in PHP (not MySQL)? Thanks!
推荐答案
我想你可以使用 date()
功能来获取这个
I think you can use date()
function to achive this
$date = '19:24:15 06/13/2013';
echo date('h:i:s a m/d/Y', strtotime($date));
这将输出
07:24:15 pm 06/13/2013
Live Sample
Live Sample
h 用于12位数字
我代表分钟
s 秒
a 将返回上午或下午(大写为AM PM)
m 用于数字的月份
d 用于数字天数
Y 大写字母用于4位数字年份(小写为两位数字)
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
m is used for months with digits
d is used for days in digit
Y uppercase is used for 4 digit year (use it lowercase for two digit)
更新
这是与 DateTime
$date = new DateTime('19:24:15 06/13/2013');
echo $date->format('h:i:s a m/d/Y') ;
这篇关于如何将日期/时间从24小时格式转换为12小时上午/下午?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!