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

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.

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

I believe the system clock on your production and dev machine is unsync, Regardless of the version of PHP or Operating System, Just like #g_bor mentioned in comment.
Don't get confused by the time zone, Just run the command date +%s on system shell to print timestamp on both machines, If you have no permission (can't login with ssh), You can prepare the PHP file which contains system('date +%s');, And then, Upload to your web server and open the browser to check the timestamps on both your machines.

Related

How to change Date and Time settings on Google Cloud Instance?

I am trying to change Date and time settings to UTC+10 Canberra,Sydney,Melbourne on the instance but it always keep rolling back to UTC+00 Monrovio, Reykjavik. Doesn't matter even if I select set time zone to automatic.enter image description here
The zone "australia-southeast1-b" on the provided screenshot is a deployment area for Google Cloud Platform resources, where the physical hosts, your VM instance is running on, are physically located. This is a geographical zone. It is not relevant to time.
To configure Date and Time in Windows, you should:
set correct time zone in Windows and
make sure a time server is reachable
Google Cloud Engine VM instance is just a virtual machine that boots up with hardware clock set to UTC as many modern servers do nowadays.
If you looked at the VM instance logs in the GCP Console you'd see that VM BIOS reports time in UTC
2019/10/3 14:9:44 Begin firmware boot time
After a while BIOS hands over to the bootloader
2019/10/3 14:9:45 End firmware boot time
Booting from Hard Disk 0...
The OS boots up. Behind the scene the OS time service recognizes the system timezone, then sets up and synchronizes time with the time source. From that time forward running programs and services report events based on the local system time:
...
2019/10/03 09:10:05 GCEWindowsAgent: GCE Agent Started (version 4.6.0#1)
In the Windows Event Log you should see entries made by the Time-Service:
Log Name: System
Source: Time-Service
Level: Information
The time provider NtpClient is currently receiving valid time data from metadata.google.internal,0x1 (ntp.m|0x1|0.0.0.0:123->169.254.169.254:123).
The time service is now synchronizing the system time with the time source metadata.google.internal,0x1 (ntp.m|0x1|0.0.0.0:123->169.254.169.254:123).
In the command prompt you can ensure that the time configuration and state are correct:
C:\Users\user>systeminfo | find /i "Time"
System Boot Time: 10/3/2019, 9:09:49 AM
Time Zone: (UTC-06:00) Central Time (US & Canada)
Hence you don't need synchronizing time neither manually or with any startup script. The time service will do this for you: synchronize the system time shortly after the system boot and keep it in sync afterwards.
All you need is to set correct Time zone and the Internet time server for Windows, and then make sure the time server is reachable via the network.
If you can't wait for the timesync cycle completion, you can logon to Windows and force time synchronization manually:
net stop W32Time
net start W32Time
w32tm /resync /force
To O.P
Answer to your question if I understand correctly, your answer is:
timedatectl set-timezone "Australia/Melbourne"

Server time error during the installation of deployment group agent via powershell

I am trying to install a deployment agent on the destination machine.
I am running the script given by Azure DevOps in the deployment group.
The agent installs successfully but then I get the error saying:
The bearer <secret removed> is not valid until {date here} 8:06:23 PM. Current server time is {same date here} 7:59:53 PM.
The server time is actually ahead (not behind) 6 minutes than my local machine. Anyway why would it matter?
And most importantly what I can do about this error? How to fix it?
Because at the end it prevents the agent from running.
Thanks
According to the error message, seems the system clock is not accurate on the agent server.
So, please update the system clock to the correct time and then try it again to check if that works.

Can't access local Sinatra server (win10 + Ubuntu on Windows)?

I can't seem to access Sinatra's local server. I have a win10 machine and I have my servers running in an Ubuntu on Windows. Sinatra has been installed without docs because installation would hang otherwise. This is the server log:
dario#DESKTOP-LSFERHU:~/dev/ruby/sinatra$ ruby first_app.rb
[2017-07-05 15:47:38] INFO WEBrick 1.3.1
[2017-07-05 15:47:38] INFO ruby 2.3.1 (2016-04-26) [x86_64-linux-gnu]
== Sinatra (v2.0.0) has taken the stage on 4567 for development with backup from WEBrick
[2017-07-05 15:47:38] INFO WEBrick::HTTPServer#start: pid=19509 port=4567
This is the app's content:
require 'sinatra'
set :bind, '0.0.0.0'
get ('/apple') do
"Here's an apple"
end
localhost:4567 hangs waiting.
I added the "set :bind" directive as a last hope, but it changes nothing. I can access no problem other local servers (for example a node server on port 5000). I turned off AVG in case, but again made no difference. Different browsers, no difference. Unsure where to go. Ideas?
It seems that the AVG antivirus, before I managed to shut it off, had somehow interfered with the server process. It somehow froze it. Any attempt I would make at starting a new server would only apparently work, while in reality conflict with this frozen process. I was unable to kill the process with any usual method (kill -KILL or so). I rebooted, added the ruby binary to the list of exceptions for AVG, and now the server works just fine. The Ubuntu on Windows had nothing to do with it.

timezone in php 5.6.x and above is not exactly?

I'm using Linux CentOS 6.6 in my VPS Server
and installed php 5.6.12 and above (php 7)
also i did config php.ini date.timezone=Europe/Amsterdam
and use in php file something this :
date_default_timezone_set('Europe/Amsterdam'); echo date("Y-m-d H:i:s");
but the time is not exactly it show me more than real time Amsterdam !!! also i checked for other timezone area but it was same problem !
for example if real time Amsterdam is 10:50:30 show me 10:58:27 !
you can see 8 min is more than real time !!!
I thought my problem was in version of php, so i updated it from php5.6.12 to php7.0.7 but it was not solved !
I am convinced this has nothing to do with php. When you enter in the shell date - what do you get?
What about contacting the server provider regarding the time difference?
To me it looks like the NTP service is not running. Check Google for NTP Service or this link.
If you have full root access to your server may be installing an NTP service would be an option.
I found this problem
I had a Webmin Control Panel on my Linux Server, so i checked System Time (in the left Menu: Hardware-> System Time) module and set Timezone to UTC, after i saw that system time was 8 min more than right time UTC !
I just set Minute and Second time according UTC world time at worldtimeserver
thank you for attention

Wait for system to sync time before performing another task

I'm using a Raspberry Pi, and upon startup it's sending an e-mail with the time and an IP address. The problem is that the time is not correct, it's the time from last time the system was shut down. When I log in through ssh and do a date command, I get the correct time. In other words, the e-mail is sent before the system has updated its time.
I was thinking of automatically running ntpdate on boot, but after reading up on it it seems like a bad idea due to the many risks of error.
So, can I somehow wait until the time has been uppdated before continuing in a script?
There is a tool included in the ntp reference implementation for this very purpose. The utility has a rather cryptic name: ntp-wait. Five minutes with the man page and you will be all set.