5
I have a problem where I need to calculate the time in a certain place in the world. The calculation should happen and the only information I have at hand is GMT of the site. EX: -1, 2, -3, etc.
I created a class to calculate hours based on the GMT. Follows:
<?php
class Data{
private $dateFormat;
private $data;
public function __construct($dateFormat = "Y-m-d H:i:s e I") {
$this->dateFormat = $dateFormat;
}
public function getHoraUsingGmt($gmt)
{
if($gmt < 0)
{
$segundos = ($gmt * 60 * 60) * -1;
$this->data = gmdate($this->dateFormat, time() - $segundos);
}
elseif($gmt == 0)
{
$this->data = gmdate($this->dateFormat, time());
}
else{
$segundos = $gmt * 60 * 60;
$this->data = gmdate($this->dateFormat, time()+$segundos);
}
return $this->data;
}
}
?>
If I use the getHoraUsingGmt method and pass a value like '-1' or '-2' the correct time is reported. However, is there any way I can calculate this and know whether or not GMT is using daylight saving time? And if so, let me know the correct time?
For example, the GMT of Brasilia is -3 (when we are not in daylight saving time). But now we are in daylight saving time and it is -2. Is there a PHP Datetime class function for this calculation?
That I know the PHP Datetime class already handles daylight saving time (I’ll look for a reference and post here). There are also Apis that you can consult for this information, such as https://developers.google.com/maps/documentation/timezone/.
– bfavaretto
I thought of creating a table in the database with all the states. Put an interval that would define the start and end date of daylight saving time (if any) and the GMT at that time. But I found "gambiarra" too much this.. rsrs (this for Brazilian state of course) For other places in the world I don’t know how I would do.
– lwb