Time calculation based on GMT and daylight saving time

Asked

Viewed 5,888 times

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/.

  • 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.

2 answers

7


The php Datetime class already handles the mentioned problems, 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 recommend using the classic functions of date and Timezone:

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 (maisculo i) returns whether it is daylight saving time or not; Check the documentation of the functions date and date_default_timezone_set for more information;

  • So if I use: $dt = new DateTime('now', new DateTimeZone(Europe/Paris')); will return me the time in Europe/Paris based on the GMT0 of Greenwich, right?

  • 1

    @lwb That’s right, just be careful to leave the Timezone in quotes, as my example;

  • Yes, I just forgot to put in my example. : ) I’ll do some tests here. Thank you.

  • I took the following test: <?php&#xA; $dt = new DateTime('2014-11-12 14:29:28', new DateTimeZone('Europe/Paris'));&#xA; echo $dt->format('Y-m-d H:i:s');&#xA;?> however no matter which Timezone I put always returns me the same time. Have to do something else? Using 'now' works well, but I will also need to convert a few hours saved to the specified Timezone. Thank you.

0

I managed to solve the problem. Follow 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!

  • Okay, I appreciate you signaling an answer as the solution to the problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.