我正在尝试获取工作时间格式.我正在使用继承的应用程序.
时间在表中存储为13:45:00
但是,它显示为1pm.我希望它显示为下午1:45
表中的数据存储在临时表中
$sql_temp="CREATE TEMPORARY TABLE IF NOT EXISTS cache (id int(11) NOT NULL,event_link text,
...
start_time varchar(255) NOT NULL,
start_time_a varchar(255) NOT NULL,
start_time来自显示为1pm的原始代码
start_time_a是我尝试显示小时数和子午线
然后读取带有开始日期的表格
$start_time=date('ga',strtotime($event->start_time));
$start_time_a = $event->start_time;
然后将其插入临时表
$ins_qu="INSERT INTO cache (id, . . . ,start_time,start_time_a) VALUES(
'".$start_time."',
'".$start_time_a."'
我提出了几种尝试使格式正确的方法
这给了我这样的结果
9pm || 21:45:00 || 6:00:21 pm CST **
晚上9点来自原始代码.
我相信21:45:00是拉弦的原始时间
下午6:00:21 CST是我尝试使用%r的strftime进行格式化的时间.
我需要怎么做才能显示hh:mm AM / PM?
解决方法:
首先通过strtotime将字符串转换为UNIX时间戳,然后使用date函数将其转换回所需的格式.
例如:
$Time = '05/12/2016 ' . $ve_n['start_time'];
$newDateTime = date('h:i A', strtotime($Time));
在您的示例中,它像:
输出为:
09:45 PM
或者使用g而不是h来显示前导零.
输出为:
9:45 PM
标签:strftime,strtotime,php
来源: https://codeday.me/bug/20191109/2012733.html