在 PHP 中获取小时和分钟
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1525921/
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
Getting Hour and Minute in PHP
提问by smakstr
I need to get the current time, in Hour:Min format can any one help me in this.
我需要以小时:分钟格式获取当前时间,任何人都可以帮助我。
回答by MattBelanger
print date('H:i');
$var = date('H:i');
Should do it, for the current time. Use a lower case h for 12 hour clock instead of 24 hour.
应该这样做,就目前而言。使用小写的 h 表示 12 小时制而不是 24 小时制。
回答by Lucas Oman
Try this:
尝试这个:
$hourMin = date('H:i');
This will be 24-hour time with an hour that is always two digits. For all options, see the PHP docs for date().
回答by fernando
print date('H:i');
You have to set the correct timezone in php.ini.
您必须在php.ini.
Look for these lines:
寻找这些行:
[Date]
; Defines the default timezone used by the date functions
;date.timezone =
It will be something like :
它会是这样的:
date.timezone ="Europe/Lisbon"
Don't forget to restart your webserver.
不要忘记重新启动您的网络服务器。
回答by Brooke.
Another way to address the timezone issue if you want to set the default timezone for the entire script to a certian timezone is to use
date_default_timezone_set()then use one of the supported timezones.
如果要将整个脚本的默认时区设置为特定时区,则解决时区问题的另一种方法是使用
date_default_timezone_set()然后使用支持的时区之一。
回答by Abdo-Host
function get_time($time) {
$duration = $time / 1000;
$hours = floor($duration / 3600);
$minutes = floor(($duration / 60) % 60);
$seconds = $duration % 60;
if ($hours != 0)
echo "$hours:$minutes:$seconds";
else
echo "$minutes:$seconds";
}
get_time('1119241');
回答by Caleb C. Adainoo
You can use the following solution to solve your problem:
您可以使用以下解决方案来解决您的问题:
echo date('H:i');
回答by GSto
In addressing your comment that you need your current time, and not the system time, you will have to make an adjustment yourself, there are 3600 seconds in an hour (the unit timestamps use), so use that. for example, if your system time was one hour behind:
在解决您需要当前时间而不是系统时间的评论时,您必须自己进行调整,一小时有 3600 秒(单位时间戳使用),因此请使用它。例如,如果您的系统时间晚了一小时:
$time = date('H:i',time() + 3600);
$time = date('H:i',time() + 3600);

