PHP:如何以小时:分钟:秒的形式获取当前时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13686139/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:53:29  来源:igfitidea点击:

PHP: How to get current time in hour:minute:second?

phpdatetime

提问by MarGa

In order to get the date in the right format I want I used date("d-m-Y"). Now I want to get the time in addition to the date in the following format H:M:SHow can I procede ?

为了以我想要的正确格式获取日期,我使用了date("d-m-Y"). 现在我想以以下格式获取除日期之外的时间H:M:S我该如何进行?

回答by Sampson

Anytime you have a question about a particular function in PHP, the easiest way to get quick answers is by visiting php.net, which has great documentation on all of the language's capabilities.

任何时候您对 PHP 中的特定功能有疑问时,获得快速答案的最简单方法是访问 php.net,该网站提供有关该语言所有功能的大量文档。

Looking up a function is easy, just visit http://php.net/<function name>and it will forward you to the appropriate place. For the date function, we'll visit http://php.net/date.

查找函数很容易,只需访问http://php.net/<function name>它,它就会将您转发到合适的位置。对于日期函数,我们将访问http://php.net/date

We immediately learn a couple things about this function by examining its signature:

通过检查它的签名,我们立即了解了关于这个函数的一些事情:

string date ( string $format [, int $timestamp = time() ] )

First, it returns a string. That's what the first stringin the above code means. Secondly, the first parameter is expected to be a string containing the format. There is an optional second parameter for passing in your own timestamp (to construct strings from some time other than now).

首先,它返回一个字符串。这就是string上面代码中第一个的意思。其次,第一个参数应该是一个包含格式的字符串。有一个可选的第二个参数用于传入您自己的时间戳(从现在以外的某个时间构造字符串)。

date("d-m-Y") // produces something like 03-12-2012

In this code, drepresents the day of the month (with a leading 0 is necessary). mrepresents the month, again with a leading zero if necessary. And Yrepresents the full 4-digit year. All of these are documented in the aforementioned link.

在此代码中,d代表月份中的第几天(必须以 0 开头)。m表示月份,如有必要,再次使用前导零。并Y代表完整的 4 位数年份。所有这些都记录在上述链接中。

To satisfy your request of getting the hours, minutes, and seconds, we need to give a quick look at the documentation to see which characters represents those particular units of time. When we do that, we find the following:

为了满足您获取小时、分钟和秒的要求,我们需要快速浏览一下文档,看看哪些字符代表了这些特定的时间单位。当我们这样做时,我们会发现以下内容:

h   12-hour format of an hour with leading zeros    01 through 12
i   Minutes with leading zeros                      00 to 59
s   Seconds, with leading zeros                     00 through 59

With this in mind, we can no create a new format string:

考虑到这一点,我们无法创建新的格式字符串:

date("d-m-Y h:i:s"); // produces something like 03-12-2012 03:29:13

Hope this is helpful, and I hope you find the documentation has benefiting to your development as I have to mine.

希望这对您有所帮助,我希望您发现文档对您的开发有所帮助,就像我必须挖掘的一样。

回答by ThomasVdBerge

You can combine both in the same date function call

您可以将两者结合在同一个日期函数调用中

date("d-m-Y H:i:s");  

回答by koopajah

You can have both formats as an argument to the function date():

您可以将两种格式作为函数 date() 的参数:

date("d-m-Y H:i:s")

Check the manual for more info : http://php.net/manual/en/function.date.php

查看手册了解更多信息:http: //php.net/manual/en/function.date.php

As pointed out by @ThomasVdBerge to display minutes you need the 'i' character

正如@ThomasVdBerge 指出的,要显示分钟,您需要“i”字符