2017-09-09 248 views
3

我如何能在24小时格式小时转换成12小时制时间在PHP像1630应该是下午4:30。转换24小时格式小时为12个小时制时间在PHP

有文章24小时制时间转换成12小时制时间16:30一样到下午4点30分,但我需要时间转换成时间。

我正在通过将它除以1200来得到余数为分钟,但我希望有另一种方法使用日期或时间函数来完成它。

+0

检查我答了'0900 0800'以及 – C2486

回答

4

我认为这会为你工作。

date("h:i a",strtotime("1630")); 
+0

是它的工作,但不是为上午9时,0800它总是显示上午1:00 –

+1

现在它将会为上午9时,0800也是如此。实际上0900自动转换为900,但现在作为一个字符串它不会。 – RamPrasadAgarwal

3

你可以像下面这样做

<?php 
$date = date("Y-m-d 16:30:00"); 
echo date("h:i a",strtotime($date)); 
?> 

现场演示:https://eval.in/858486

或者,如果你的时间是1630

$time = 1630; 
$time = (int)(1630/100).":".(1630%100); 
$date = date("Y-m-d $time:00"); 
echo date("h:i a",strtotime($date)); 

现场演示:https://eval.in/858559

更新3位号码如920815

$time = "0920"; // here $time should be string `0` is for other base of number 
$last2 = substr($time, -2); 
$time = str_replace($last2,":".$last2,$time); 
$date = date("Y-m-d $time"); 
echo date("h:i a",strtotime($date)); 

现场演示:https://eval.in/858555

相关问题