gpt4 book ai didi

PHP - 格式化 HH :MM:SS to 12hr HH:MM:a string

转载 作者:行者123 更新时间:2023-11-29 09:00:15 28 4
gpt4 key购买 nike

我希望这对某人来说是小菜一碟!目前,所有内容的字符串输出均为上午 12:00。

以下来自 MySQL 的代码,格式为 HH:MM:SS (hours_open, hours_close)

$get_hours_sql = "SELECT * FROM client_hours ORDER BY day"; 
$get_hours_res = mysqli_query($dbConnect, $get_hours_sql) or die(mysqli_error($dbConnect));

// Establish the output variable
$hoursList = '<div class="right_bar">';
while ($productList = mysqli_fetch_array($get_hours_res)) {
$id_hours = $productList['id_hours'];
$day = $productList['product_name'];
$open = $productList['hours_open'];
$close = $productList['hours_close'];

$hoursList .= ''.date("g:ia", $open).' - '.date("g:ia", $close).'<br/>';
}

$hoursList .= '</div>';

echo $hoursList;

当前输出

12:00am - 12:00am

循环播放。

我想将输出输出到

11:00am - 11:00pm

这将代表数据库条目。

谢谢!

最佳答案

我总是发现 PHP <-> MySql 日期处理很繁琐(不过 5.3 的情况更好了)。

我的猜测是 mysql 查询以字符串形式返回日期,而 date() 需要时间戳。

通常,我只是让 mysql 将日期格式化为字符串,并将其作为附加字段返回,如下所示:

$get_hours_sql = "SELECT *,date_format(hours_open,'%h:%i %p') as hours_open_formatted, date_format(hours_close,'%h:%i %p') as hours_close_formatted  FROM client_hours ORDER BY day"; 

然后只需使用格式化字段:

$hoursList .= ''.$productList['hours_open_formatted'].' - '.$productList['hours_close_formatted'].'<br/>';

关于PHP - 格式化 HH :MM:SS to 12hr HH:MM:a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8844499/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com