Hi
The website MJU20 was using (worldtimeapi.org) to obtain current time appears to be very, very hit or miss. Numerous times I can't even reach it using a browser, and if you search online for anything about it, you read numerous comments about it being "down".
With that in mind I thought to look at an alternative. Whilst most websites return current time as part of their headers, you shouldn't really make a spurious request to a website in an attempt to grab the time from their returned header.......
If you have a server of your own, either in your own premises or hosted, then it is quite easy to deploy a PHP script to tell you the time whenever you wish. I have a few hosted servers and also a RPi one in my house so I decided that was the route to follow. Do appreciate that this example is only an example and it may or may not suit you. It takes no account of latency and you could perhaps modify to establish the average latency and calculate locally to minimise. However I think in most cases +/- a few hundred milliseconds would be accurate enough <s>.
My servers run on GMT (or UTC if you prefer) but you should check yours before deploying and make whatever adjustments may be needed.
Although running on GMT, which is no issue for me, I thought I'd add the capability to request a timezone "offset" in the request. To make the request we create a "GET" request and this contains the script to be run plus the "offset". The offset is floating point as you may be in one of those zones that are "funny". You may be GMT+5.5 for example. If you want GMT then you would specify 0.0. If West of London then you may be using -4.0 or suchlike.
The script accepts the request and obtains the offset and current Unix Time. As each hour has 3600 seconds, we then do a calculation to establish the time at offset by multiplying 3600 (number of seconds in one hour) by the offset, and adding this to the Unix Time.
We then create a json encoded reply based on the this new Unix Time.
I chose to return the following:-
Weekday = Day of week (e.g. Saturday)
Day = Day of Month (e.g. 17)
Month = Month of Year (e.g. 06)
Year = Year (e.g. 2025)
Hour = Hour in 24hr format (e.g. 17 equals 5pm)
Minutes = Minutes (e.g. 24)
Seconds = Seconds (e.g. 26)
Once this has been returned you can use the info to either sync locally or action whatever, whenever.
The attached pdf contains the script steps but do be aware that no security is depolyed which may or may not be a concern for you.
The attached FlowChart is written for a RPi but you can modify to suit. In it we need to set some parameters such as the server address, the script to run (e.g. script.php) and of course the offset. We then initialse components, call the "update" macro, then upon exiting enter the Main loop (which does nothing).
Basically you could add in the Update_Time Macro to your project to provide updates whenever you wish.
There are other ways to manipulate PHP date and time functions but this I thought was quite easy especially if you want a timezone not included in PHP.
Hopefully you may find this of interest.
Regards