作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望这对某人来说是小菜一碟!目前,所有内容的字符串输出均为上午 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/
我是一名优秀的程序员,十分优秀!