I'm using openweathermap api to get weather from different location. from this I receive :
"sys":{"type":2,"id":2008110,"country":"CH","sunrise":1633585192,"sunset":1633626144},"timezone":7200
But i don't know How to get localtime using php...
CodePudding user response:
The API provides a UTC time stamp and a time zone offset.
$sunrise = 1633585192;
$timezoneOffset = 7200; //Seconds
If a DateTime object is created from a time stamp, the time zone is always UTC.
$dt = date_create("@".$sunrise);
To get the local time, the object has to be transformed into the correct time zone.
$hhmm = sprintf("% 03d:d",(int)($timezoneOffset/3600),$timezoneOffset%3600); // 02:00
$dt->setTimeZone(new DateTimeZone($hhmm));
//"2021-10-07 07:39:52.000000"
Now the object has the desired local time and the correct time zone.