如何输出2.30
之类的值?目前我得到的输出是2.3
。以下是我的代码,其中24小时时间格式将转换为12小时格式:
if($openHrs['open_hours'] == NULL){
} else {
$openHrs = explode(",", $openHrs['open_hours']);
$weekdays = array('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$res = array();
$todayWeekDay = date('D'); // get today's weekday
$todayWeekNum = array_search($todayWeekDay, $weekdays); // get number for today's weekday
foreach($openHrs as &$temp) {
$temp = explode(" ", $temp);
$temp[1] = $temp[1] > 12 ? $temp[1] - 12 . ' pm' : $temp[1] . ' am';
$temp[2] = $temp[2] > 12 ? $temp[2] - 12 . ' pm' : $temp[2] . ' am';
// only add the item where the weekday is equal to today's
if ($temp[0] == $todayWeekNum) {
$res[] = $weekdays[$temp[0]] . ' ' . $temp[1] . ' - ' . $temp[2];
}
}
}
问题是,2.3
代替2.30
获得14.30
小时格式。我在这里缺少什么?
答案 0 :(得分:2)
问题是PHP将您的时间视为浮点数!
您可以将其转换为字符串,或尝试sprintf("%0.2f", $output_variable)
应该在小数点后用两位小数格式化它。
答案 1 :(得分:2)
您可以使用 number_format($your_number, 2, '.', '');
显示最多2个十进制字符的数字。