Time calculation based on GMT and daylight saving time

5

I have a problem where I need to calculate the time in a particular place in the world. The calculation should happen and the only information I have in hand is GMT of the site. EX: -1, 2, -3, etc.

I've created a class to calculate GMT-based hours. Following:

<?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 such as '-1' or '-2' the correct time is reported. But is there any way I can figure this out and know if GMT is using daylight saving time or not? And if so, tell me the correct time?

For example, the Brasilia GMT is -3 (when it's not in daylight saving time). But now it's summer time and it's -2. Is there a function of the PHP DateTime class for this calculation?

    
asked by anonymous 10.12.2014 / 19:19

2 answers

7

The DateTime class of php already handles the problems mentioned, just pass a timezone that it will convert correctly;

// Pega a data atual com base no horario de Sao Paulo
$dt = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));

If you can not use it, I recommend using the classic date and timezone functions:

date_default_timezone_set('America/Sao_Paulo'); // Seta a timezone
$horarioVerao = date('I', $timeStamp); // Retorna se está em horário de verao ou não;

Parameter I (uppercase i) returns whether it is summer time or not; Check the date documentation and date_default_timezone_set for more information;

    
11.12.2014 / 11:01
1

I was able to solve the problem. Here is the code I used in the solution!

<?php
$data_inicio = '2014-11-12 15:01:01';
$original_timezone = new DateTimeZone('UTC');

$datatime = new DateTime($data_inicio, $original_timezone);

$novo = new DateTimeZone('America/Sao_Paulo');
$datatime->setTimeZone($novo);

$output = $datatime->format("Y-m-d H:i:s");
echo $output;
?>

Thanks for the help!

    
11.12.2014 / 18:33