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

查看:39
本文介绍了如何在 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 中用 date.timezone 设置.ini,或在脚本中使用 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"是,所以我不得不添加一个 if 来解决这个问题.

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天全站免登陆