duanrao3371
duanrao3371
2012-06-22 12:40
浏览 25
已采纳

PHP在Windows中,如何获取忽略时区的当前系统时间?

I have the next problem. I have several PHP systems running in different Windows machine. Those systems use intensivelly the current time.

Right now, i configured php.ini and assigned it to a specific timezone. Also Windows is configure to the same timezone and everything works as expected.

However, in my country, the State decided to change the daylight saving time. So, sometimes, the admin changes the windows timezone and left unchanged the php timezone, creating a discordance in the system. Other times, is windows who changes automatically the timezone.

Is there are any way, from PHP, to obtain the current system time that ignores the timezone?.

update:

function time_zone_fix($timeGiven = "H:i:s")
{
    $shell = new COM("WScript.Shell") or die("Requires Windows Scripting Host");
    $time_bias = -($shell->RegRead("HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\TimeZoneInformation\\ActiveTimeBias")) / 60;
    $timestamp_bias = 60 * 60 * $time_bias;
    return gmdate($timeGiven, strtotime($timestamp_bias . " seconds"));
}

gives the current date and time no matter what timezone/dsl is specified. However, creating a new COM inteface is anything but efficient.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doubao12345
    doubao12345 2012-06-22 12:54
    已采纳
    gmdate('Y-m-d H:i:s');
    

    changing the format to whatever you need. It returns the "timezone-less date-and-time". The gm stands for Greenwich Mean, as in GMT (Greenwich Mean Time). GMT has been superseded by UTC (Universal Time, Coordinated), so the function should really be called utcdate()...

    But the most portable representation of time is the Unix timestamp, i.e., the number of seconds elapsed since 1970-01-01 00:00:00 UTC, ignoring leap seconds:

    time();
    

    The value of time() is also the default value for the second parameter of gmdate().

    BTW, a leap second is scheduled for 2012-06-30 23:59:60

    Note: In case you are asking for a way to get the time at a certain timezone but without the daylight saving time correction, you can do this:

    gmdate('Y-m-d H:i:s', time() + 3600 * $h);
    

    where $h is the offset in hours from UTC (a negative number in America), or, in case the offset is not a full number of hours:

    gmdate('Y-m-d H:i:s', time() + 60 * $m);
    

    where $m is the offset in minutes from UTC (e.g., 330 for IST, India Standard Time). A Unix timestamp with an added timezone offset doesn't make any sense by itself, though!

    Last but not least, don't forget to synchronize your systems by means of NTP, the Network Time Protocol.

    点赞 评论
  • dousuie2222
    dousuie2222 2012-06-22 12:45

    PHP Manual List of Supported Timezones:

    ... Here you'll find the complete list of timezones supported by PHP, which are meant to be used with e.g. date_default_timezone_set(). ...

    And then this one: UTC (from this sub-page)

    点赞 评论
  • doumuyu0837
    doumuyu0837 2013-09-19 06:07

    PHP in Windows, how to obtain the current system time that ignores the timezone? I have the next problem. I have several PHP systems running in different Windows machine. Those systems use intensivelly the current time.

    点赞 评论

相关推荐