Why does the php 'date' function return the wrong time (off for ~ 24 seconds)?

I have the following small phpsnippet running on gentooLinux (php version 5.2.10-pl0-gentoo):

#!/usr/bin/php5
<?

class TestDaemon {
    public function __construct(){

        while (TRUE){

            unset($aDate);
            exec("date", $aDate);
            print("date(\"d.m.y H:i:s\") yields: ".date("d.m.y H:i:s")." while 'date' yields $aDate[0].\n");
            sleep(1);
        }
    }
}
$oDaemon = new TestDaemon();
?>

And the result is as follows:

date("d.m.y H:i:s") yields: 27.03.14 07:05:27 while 'date' yields Thu Mar 27 07:05:03 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:28 while 'date' yields Thu Mar 27 07:05:04 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:29 while 'date' yields Thu Mar 27 07:05:05 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:30 while 'date' yields Thu Mar 27 07:05:06 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:32 while 'date' yields Thu Mar 27 07:05:07 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:33 while 'date' yields Thu Mar 27 07:05:09 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:34 while 'date' yields Thu Mar 27 07:05:10 UTC 2014.
date("d.m.y H:i:s") yields: 27.03.14 07:05:35 while 'date' yields Thu Mar 27 07:05:11 UTC 2014.

As you can see, the time is off for approx. 24 seconds. On another machine (the same OS, the same version of PHP), I do not see such an offset.

What is the reason for this bias? Is this due to the leap of the second differences? Then which system gives the right time? Why phpnot use the Linux system time instead?

In addition, can this time offset be a source of problems when working with the mysql database on the same system?

+4
3

, shell running/bin/date "" , php POSIX. 25 , tz , 24 . , http://www.ucolick.org/~sla/leapsecs/amsci.html . "" . POSIX , . [edit, , ]

+4

, , php date() time() . time() :

, , Unix (1 1970 00:00:00 ).

, .

SO time() date.timezone, php.ini date_default_timezone_set().
, , php.ini, , php, , .

, :

, Linux, , Windows. Linux /etc/timezone. open_basedir, :/etc/timezone , . , , , script . , , 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, .
0

, Linux :

  • (, RTC BIOS), . , , , . . hwclock
  • , . (cat /proc/uptime)
  • , NTP ntpd.
  • / , . . NTP (/var/lib/ntp/drift/ntp.drift ), , .
  • 1 1970 . . date +%s
  • , /etc/localtime, . zdump /etc/localtime
  • The current system time calculated using all of the above plus the rules for time zone, leap year, second jump, etc. Cm.date

If I were to guess, I would say that NTP slowly adjusts your system time to compensate for your distorted real-time clock, while PHP bypasses this and gets in the way of it somewhere else.

0
source

Source: https://habr.com/ru/post/1533666/


All Articles