ubuntu-16.04php-7.0

PHP time() returns a timestamp that is 59 seconds behind real time


I have a dev environment and a production environment. The time() function returns the correct timestamp in my dev env but in my production environment time() is exactly 59 seconds behind! The version of PHP is 7.0.3 on both environments. This can't be a timezone issue since the difference is only one minute and the PHP default timezone is the same in both environments (America/Los Angeles). It is now 10:48:29 am and here is the output from calling this function simultaneously in both environments (or at least as fast as I can press enter on the console).

Dev environment (is correct)

php > echo date('h:i:s A');
10:48:29 AM

Production environment (is one minute behind)

echo date('h:i:s A');
10:47:31 AM

Same happens with the time() function

Dev environment (is correct)

php > echo time();
1568742851

Production environment (is one minute behind)

echo time();
1568742792

And 1568742851 - 1568742792 = 59 secs

I know i could just add 59 seconds to the timestamp time() returns but that just seems like a hack and does not solve the problem. I would like to please get advice as to how to fix my production environment.


Solution

  • Confirm that you are allowing NTP traffic to your production OS environment. You can confirm that your server is properly communicating to NTP by running the following command ntpq -c peers If you receive a Connection refused prompt you don't have proper connectivity. You will need to configure an inbound firewall rule allowing UDP traffic on port 123. Once this is done, restart your NTP service service ntpd restart and rerun the ntpq -c peers command