如何在 PHP 中获取本地系统时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2329467/
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 get the local system time in PHP?
提问by JohnWithoutArms
I'm writing a PHP system and I need to get the system time. Not the GMT time or the time specific to a timezone, but the same system time that is used by the CRON system. I have a CRON job that runs every day at midnight and I want to show on a webpage how long will it take before it runs again.
我正在编写一个 PHP 系统,我需要获取系统时间。不是 GMT 时间或特定于某个时区的时间,而是 CRON 系统使用的相同系统时间。我有一个每天午夜运行的 CRON 作业,我想在网页上显示它需要多长时间才能再次运行。
For example: Right now it is 6pm on my system clock. I run the code:
例如:现在我的系统时钟是下午 6 点。我运行代码:
$timeLeftUntilMidnight = date("H:i", strtotime("tomorrow") - strtotime("now"));
The result, however, is "3:00" instead of "6:00". If I run
然而,结果是“3:00”而不是“6:00”。如果我跑
date("H:i", strtotime("tomorrow"));
It returns 0:00, which is correct. But if I run
它返回 0:00,这是正确的。但如果我跑
date("H:i", strtotime("now"));
It returns 21:00, even though the correct should be 18:00.
它返回 21:00,即使正确的应该是 18:00。
Thanks.
谢谢。
回答by knittl
回答by stivlo
There are many answers, however there is not even one correct at the time of writing.
有很多答案,但在撰写本文时甚至没有一个是正确的。
PHP time()function doesn't return the system time, like most folks believe, but it return the PHP localtime, normally set with date.timezonein php.ini, or set with date_default_timezone_set()within a script.
PHPtime()函数并不像大多数人相信的那样返回系统时间,但它返回 PHP 本地时间,通常date.timezone在 php.ini 中设置,或date_default_timezone_set()在脚本中设置。
For instance in one of my servers, PHP time was set to Europe/Romeand system time to UTC. I had a difference of one hour between system time and PHP time.
例如,在我的一台服务器中,PHP 时间设置为Europe/Rome,系统时间设置为UTC. 我的系统时间和 PHP 时间相差一小时。
I'm going to give you a solution that works for Linux, I don't know for Windows. In Linux the system timezone is set in /etc/timezone. Now, this is normally outside my allowed open_basedirsetting, but you can add :/etc/timezoneto your list to be able to read the file.
我会给你一个适用于 Linux 的解决方案,我不知道适用于 Windows。在 Linux 中,系统时区设置在/etc/timezone. 现在,这通常超出了我允许的open_basedir设置,但您可以添加:/etc/timezone到您的列表中以便能够读取该文件。
Then, on top of the scripts, that want to get the system time, you can call a library function that sets the script timezone to the system timezone. I suppose that this function is part of a class, so I use static:
然后,在想要获取系统时间的脚本之上,您可以调用将脚本时区设置为系统时区的库函数。我想这个函数是一个类的一部分,所以我使用静态:
static function setSystemTz() {
$systemTz = trim(file_get_contents("/etc/timezone"));
if ($systemTz == 'Etc/UTC') $systemTz = 'UTC';
date_default_timezone_set($systemTz);
}
To make the matter worse in PHP 5.3.3 'Etc/UTC' is not recognized, while 'UTC' is, so I had to add an if to fix that.
更糟糕的是,PHP 5.3.3 无法识别“Etc/UTC”,而“UTC”是,所以我不得不添加一个 if 来解决这个问题。
Now you can happily call time()and it will really give you the system time. I've tested it, because I needed it for myself, that's why I found this question now.
现在你可以愉快地打电话了time(),它真的会给你系统时间。我已经测试过了,因为我自己需要它,这就是我现在发现这个问题的原因。
回答by Justin Vincent
This is the easiest and most foolproof way to do it:
这是最简单和最简单的方法:
$sys_timestamp = strtotime(exec("date"));
Let's not try to spoof it with php, let's just get the real sys time ;)
我们不要试图用 php 欺骗它,让我们获得真正的系统时间;)
(Will work on any unix based system)
(适用于任何基于 Unix 的系统)
回答by Bhautik Nada
You can current local time via below Javascript and set it in any PHP variable than.
您可以通过下面的 Javascript 获取当前本地时间,并将其设置在任何 PHP 变量中。
<script>
// For 24 hours
var ct = new Date();
var hr = ct.getHours();
var mt = ct.getMinutes();
if (mt < 10) {
mt = "0" + mt;
}
document.write("<b>" + hr + ":" + mt + " " + "</b>");
// For 12 hours (AM / PM)
var ct = new Date();
var hr = ct.getHours();
var mt = ct.getMinutes();
var ampm = "AM";
if (hr >= 12) {
ampm = "PM";
hr = hr - 12;
}
if (hr == 0) {
hr = 12;
}
if (mt < 10) {
mt = "0" + mt;
}
document.write("<b>" + hr + ":" + mt + " " + ampm + "</b>");
</script>
回答by jo carlo
try this one:
试试这个:
$time=date("h:i:s A", strtotime("now"-14));
echo $time;
You can adjust the time by changing the number 14above.
您可以通过更改14上面的数字来调整时间。
回答by Sanjay Singh
For getting the current time of your system you need to set the correct date.timezonein your php.ini file. For example if you are from India then you would write:
要获取系统的当前时间,您需要date.timezone在 php.ini 文件中设置正确的时间。例如,如果你来自印度,那么你会写:
date.timezone = Asia/Calcutta
For Germany, it would be:
对于德国,它将是:
date.timezone = Europe/Berlin
After doing this, date("Y-m-d H:i:s")will give your current time. For getting your timezone see the list of timezones supported by PHP.
这样做后,date("Y-m-d H:i:s")将给出您当前的时间。要获取您的时区,请参阅PHP 支持的时区列表。
回答by Pekka
回答by Pascal MARTIN
You can get the date/time of the server on which PHP is running using the time()function -- it'll return a timestamp, that corresponds to the current datetime.
您可以使用该time()函数获取运行 PHP 的服务器的日期/时间——它将返回一个时间戳,与当前日期时间相对应。
It's the system time, on that server -- the same as used by cron.
这是该服务器上的系统时间——与 cron 使用的相同。
回答by ZeroUptime
If you want the GMT time you may want to use gmtstrftime(), which will give you the system time but as in GMT. There's more info at http://us2.php.net/gmstrftime.
如果您想要 GMT 时间,您可能需要使用gmtstrftime(),这将为您提供系统时间,但与 GMT 一样。http://us2.php.net/gmstrftime 上有更多信息。
回答by Jake Worrell
If you are after a formatted date:
如果您在格式化日期之后:
date ('d/m/y h:i:s');
will do the trick, there is no need to pass time() into date as it will default to the current system time if no second parameter is supplied.
可以解决这个问题,不需要将 time() 传递给 date 因为如果没有提供第二个参数,它将默认为当前系统时间。
for more formatting options see here: http://php.net/manual/en/function.date.php
有关更多格式选项,请参见此处:http: //php.net/manual/en/function.date.php
Otherwise you can just use
否则你可以使用
time()
To get you the current unix timestamp as others have mentioned.
为您提供其他人提到的当前 unix 时间戳。

