Page 1 of 1

ESP32 set time offset

Posted: Tue May 04, 2021 11:56 am
by buster
I want to load the time from my own server and set the ESP32 clock to the received time.
The time from my server is a php script that returns this:

Code: Select all

<?PHP
	echo strtotime(date('Y-m-d H:i:s'));
?>
The EPS32 receives the time and sets the internal clock like this:

Code: Select all

struct timeval tv;
tv.tv_sec = atol(receivedTime);
settimeofday(&tv, NULL);
I don't want to set a timezone because every device can be placed around the world where there are different timezones so i want my php script to take care of that (in the future).

The only problem is that the EPS has 1 hour and 43 minutes time difference than the current time.
PHP shows the right time but the EPS converts it "wrong" to display the correct time.

To show the time, i use this code:

Code: Select all

struct tm timeinfo;
if(!getLocalTime(&timeinfo)){
  Serial.println("Failed to obtain time");
  return;
}
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
If i add 6130 seconds to the php time, i get the correct time in the ESP32

What am i doing wrong here?

Re: ESP32 set time offset

Posted: Tue May 04, 2021 1:39 pm
by lbernstone
Use gmdate instead of date in your php.

Re: ESP32 set time offset

Posted: Tue May 04, 2021 2:29 pm
by buster
This doesn't make any difference, also it doesn't look like it is the timezone, i have a time difference of 1 hour and 43 minutes, it is not a full hour or 2 full hours