php 我如何在php中格式化时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5331481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do i format time in php
提问by Someone
I have a time returned from database in php as 92500. But i want to format the time as 09:25 how can do this echo date ('H:i',strtotime($row['time']))
.it is outputting 00:00. How can i get 09:25
我有一个时间从 php 中的数据库返回为 92500。但我想将时间格式化为 09:25 如何做到这一点。echo date ('H:i',strtotime($row['time']))
它输出 00:00。我怎样才能得到 09:25
回答by Alex Bailey
Actually
实际上
$date = '9:25';
echo date ('H:i',strtotime($date));
is working perfectly fine for me.
对我来说工作得很好。
Returns "09:25".
返回“09:25”。
So i guess it has to be some error with your database value meaning $row['time'] doesn't contain the right value.
所以我猜你的数据库值一定有错误,这意味着 $row['time'] 不包含正确的值。
回答by sfg2k
public static function formatSeconds($secs=0){
$units = array('Sec', 'Min', 'Hrs', 'Days', 'Months', 'Years');
if($secs<60){
$time=$secs;
$pow=0;
}
else if($secs>=60 && $secs<3600){
$time=$secs/60;
$pow=1;
}
else if($secs>=3600 && $secs<86400){
$time=$secs/3600;
$pow=2;
}
else if($secs>=86400 && $secs<2592000){
$time=$secs/86400;
$pow=3;
}
else if($secs>=2592000 && $secs<31104000){
$time=$secs/2592000;
$pow=4;
}
else if($secs>=31104000 ){
$time=$secs/31104000;
$pow=5;
}
return round($time) . ' ' . $units[$pow];
}
回答by zod
g 12-hour format of an hour without leading zeros 1 through 12
g 12 小时制,不含前导零 1 到 12
Use PHP date itself
使用 PHP 日期本身
回答by Rocket Hazmat
You said $row['time']
was "number type". Do you mean that it's a timestamp? If so, you don't need strtotime
.
你说的$row['time']
是“数字类型”。你的意思是这是一个时间戳?如果是这样,您不需要strtotime
.
echo date('H:i', $row['time'])
echo date('H:i', $row['time'])
The value 92500
is not a valid time value for strtotime()
. See this pagefor valid time values.
该值92500
不是 的有效时间值strtotime()
。有关有效时间值,请参阅此页面。
回答by Matthew
One of many ways:
多种方式之一:
$time = '92500'; // HHMMSS
if (strlen($time) == 5)
$time = '0'.$time;
echo substr($time, 0, 2).':'.substr($time, 2, 2);
回答by Rajat Jain
Try this simple function to convert 24 hours time to 12 hour time including "AM" and "PM"
试试这个简单的函数,将 24 小时制转换为 12 小时制,包括“AM”和“PM”
<?php
echo change_time("00:10"); // call the change_function("user input here")
?>
function change_time($input_time)
{
// 23:24
//break time
$hours = substr($input_time,0,2);
$mins = substr($input_time,3,2);
if (($hours >= 12) && ($hours <= 24))
{
if (($hours == 24))
{
$new_hour = "00";
$part = "AM";
}
else {
$new_hour = $hours - 12;
$part = "PM";
}
}
else
{
//$new_hour = $hours - 12;
$new_hour = $hours;
$part = "AM";
}
return $new_hour .":" . $mins ." " . $part . "(".$input_time .")";
}