ESP32 set time offset

buster
Posts: 15
Joined: Sun Jan 24, 2021 11:43 am

ESP32 set time offset

Postby buster » Tue May 04, 2021 11:56 am

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?

lbernstone
Posts: 635
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 set time offset

Postby lbernstone » Tue May 04, 2021 1:39 pm

Use gmdate instead of date in your php.

buster
Posts: 15
Joined: Sun Jan 24, 2021 11:43 am

Re: ESP32 set time offset

Postby buster » Tue May 04, 2021 2:29 pm

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

Who is online

Users browsing this forum: No registered users and 70 guests