如何在PHP中获取本地系统时间? [英] How do I get the local system time in PHP?

查看:189
本文介绍了如何在PHP中获取本地系统时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个PHP系统,我需要获得系统时间。不是GMT时间或特定于时区的时间,而是CRON系统使用的相同系统时间。我有一个CRON作业,每天在午夜运行,我想在一个网页上显示它需要多长时间才能再次运行。

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.

例如:
现在是我的系统时钟下午6点。我运行代码:

For example: Right now it is 6pm on my system clock. I run the code:

$timeLeftUntilMidnight = date("H:i", strtotime("tomorrow") - strtotime("now"));

但结果是3:00而不是6:00。如果我运行

The result, however, is "3:00" instead of "6:00". If I run

date("H:i", strtotime("tomorrow"));

它返回0:00,这是正确的。但如果我运行

It returns 0:00, which is correct. But if I run

date("H:i", strtotime("now"));

它会返回21:00,即使正确的时间是18:00。

It returns 21:00, even though the correct should be 18:00.

感谢。

推荐答案

有很多答案,写入时间。

There are many answers, however there is not even one correct at the time of writing.

PHP time()函数不返回系统时间, ,但它返回PHP本地时间,通常在php.ini中设置 date.timezone ,或设置 date_default_timezone_set()

PHP time() function doesn't return the system time, like most folks believe, but it return the PHP localtime, normally set with date.timezone in php.ini, or set with date_default_timezone_set() within a script.

例如,在我的某个服务器中,PHP时间设置为 Europe / Rome 系统时间到 UTC 。我在系统时间和PHP时间之间有一个小时的差别。

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.

我会给你一个适用于Linux的解决方案,我不知道为Windows 。在Linux中,系统时区设置在 / etc / timezone 中。现在,这通常在我允许的 open_basedir 之外,但您可以添加:/ etc / timezone 到您的列表能够读取文件。

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_basedir setting, but you can add :/etc/timezone to your list to be able to read the file.

然后,在脚本之上,想要获得系统时间,可以调用一个库函数将脚本时区设置为系统时区。我假设这个函数是类的一部分,所以我使用静态:

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);
}

在PHP 5.3.3'Etc / UTC'不能识别,而'UTC'是,所以我不得不添加一个如果修复。

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.

现在你可以高兴地调用 time(),它会真的给你系统时间。我测试了它,因为我需要它为自己,这就是为什么我现在找到这个问题。

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.

这篇关于如何在PHP中获取本地系统时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆