• <bdo id='LSauF'></bdo><ul id='LSauF'></ul>
    1. <tfoot id='LSauF'></tfoot>
      <legend id='LSauF'><style id='LSauF'><dir id='LSauF'><q id='LSauF'></q></dir></style></legend>

      1. <small id='LSauF'></small><noframes id='LSauF'>

        <i id='LSauF'><tr id='LSauF'><dt id='LSauF'><q id='LSauF'><span id='LSauF'><b id='LSauF'><form id='LSauF'><ins id='LSauF'></ins><ul id='LSauF'></ul><sub id='LSauF'></sub></form><legend id='LSauF'></legend><bdo id='LSauF'><pre id='LSauF'><center id='LSauF'></center></pre></bdo></b><th id='LSauF'></th></span></q></dt></tr></i><div id='LSauF'><tfoot id='LSauF'></tfoot><dl id='LSauF'><fieldset id='LSauF'></fieldset></dl></div>

      2. 如何在 PHP 中获取本地系统时间?

        时间:2023-08-18

        • <bdo id='a7IZK'></bdo><ul id='a7IZK'></ul>
        • <legend id='a7IZK'><style id='a7IZK'><dir id='a7IZK'><q id='a7IZK'></q></dir></style></legend>

                <small id='a7IZK'></small><noframes id='a7IZK'>

                <tfoot id='a7IZK'></tfoot>
                    <tbody id='a7IZK'></tbody>
                  <i id='a7IZK'><tr id='a7IZK'><dt id='a7IZK'><q id='a7IZK'><span id='a7IZK'><b id='a7IZK'><form id='a7IZK'><ins id='a7IZK'></ins><ul id='a7IZK'></ul><sub id='a7IZK'></sub></form><legend id='a7IZK'></legend><bdo id='a7IZK'><pre id='a7IZK'><center id='a7IZK'></center></pre></bdo></b><th id='a7IZK'></th></span></q></dt></tr></i><div id='a7IZK'><tfoot id='a7IZK'></tfoot><dl id='a7IZK'><fieldset id='a7IZK'></fieldset></dl></div>
                • 本文介绍了如何在 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 中获取本地系统时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:通过 crontab 一个变量并从 PHP 读取它? 下一篇:每 24 小时运行一次 php 任务

                  相关文章

                    <bdo id='nrmoH'></bdo><ul id='nrmoH'></ul>

                  <small id='nrmoH'></small><noframes id='nrmoH'>

                  <legend id='nrmoH'><style id='nrmoH'><dir id='nrmoH'><q id='nrmoH'></q></dir></style></legend>
                  1. <tfoot id='nrmoH'></tfoot>
                    1. <i id='nrmoH'><tr id='nrmoH'><dt id='nrmoH'><q id='nrmoH'><span id='nrmoH'><b id='nrmoH'><form id='nrmoH'><ins id='nrmoH'></ins><ul id='nrmoH'></ul><sub id='nrmoH'></sub></form><legend id='nrmoH'></legend><bdo id='nrmoH'><pre id='nrmoH'><center id='nrmoH'></center></pre></bdo></b><th id='nrmoH'></th></span></q></dt></tr></i><div id='nrmoH'><tfoot id='nrmoH'></tfoot><dl id='nrmoH'><fieldset id='nrmoH'></fieldset></dl></div>