Add custom timezones in PHP?

Asked

Viewed 471 times

10

TL;DR

Is there any way to add timezones customized in PHP?

Goal

I would like to be able to register a schedule (using new DateTime(...)) with an arbitrary time offset value (Timezone), for example 13 minutes, 51 minutes or 3 hours and 27 minutes, to use the method DateTime::diff() to calculate the time interval between two events recorded by two separate watches which may be out of sync with each other by an arbitrary (but known) time difference.

Practical example:

Event 1: 17h40min (registered per clock 1)

Event 2: 19:40 (recorded per clock 2, which is 4 minutes behind the clock 1)

Time interval between events: 2 hours and 4 minutes.

3 answers

6

Maybe what you could use, beyond the possibility that the mgibsonbr indicated, is the Modify:

So I could change the Datetime object to give the modified date with the difference inserted.

Example:

$data1 = new DateTime();
$data2 = new DateTime();
$data2->modify("+124 minutes");

$diferenca = $data2->diff($data1);
$resultado = $diferenca->format('%y Anos %m meses %a dias %h horas %i minutos %S segundos');
echo $resultado; // dá 0 Anos 0 meses 0 dias 2 horas 4 minutos 00 segundos

Example

  • 2

    I didn’t know that modify, in fact it is much simpler than my solution. + 1

3

I don’t know any way to create timezones customized, but I can suggest an alternative using timestamps.

  1. Convert your date to a timestamp (int), via strtotime (if your date is in format string) or DateTime::getTimestamp (if it is in that format):

    $timestamp1 = strtotime($hora1);
    $timestamp2 = $hora2->getTimestamp();
    
  2. Adjust difference in seconds of one of them (or both):

    function ajustar($timestamp, $horas = 0, $minutos = 0, $segundos = 0)
    {
        return $timestamp + ($segundos + 60 * ($minutos + 60 * $horas);
    }
    
    $timestamp1_ajustado = ajustar($timestamp1);
    $timestamp2_ajustado = ajustar($timestamp1, 0, 4);
    
  3. Convert them back to one DateTime, using DateTime::setTimestamp:

    $hora1_ajustada = new DateTime();
    $hora1_ajustada->setTimestamp($timestamp1_ajustado);
    
    $hora2_ajustada = new DateTime();
    $hora2_ajustada->setTimestamp($timestamp2_ajustado);
    

Now you can calculate the difference between them normally. Full code:

function diferenca_ajustada($hora1, $hora2, $horas = 0, $minutos = 0, $segundos = 0)
{
    $timestamp1 = $hora1->getTimestamp();
    $timestamp2 = $hora2->getTimestamp() + $segundos + 60*($minutos + 60*$horas);

    $ajustada1 = new DateTime();
    $ajustada1->setTimestamp($timestamp1);
    $ajustada2 = new DateTime();
    $ajustada2->setTimestamp($timestamp2);

    return $ajustada1->diff($ajustada2);
}

Example in Phpfiddle. Note: if dates are in different timezones, you may first need to convert them to UTC before comparing them:

function para_utc($timestamp)
{
    return $timestamp - date("Z", $timestamp);
}
  • Man, thank you so much for your answer, you showed a lot of effort. Currently, what I’m doing is more or less the same: I’m compensating the offsets manually. However, the idea of the question is to try to figure out how to do this using the Datetime class and its Datetime::diff() method, which would be much cleaner and more organized, and which is only possible by setting arbitrary timezones.

  • Anyway, you answered my question in the first row ("I don’t know any way to create custom timezones"), so if no one else knows any way to do that, I mark your answer as you accept.

  • 1

    @I imagined this, but as I searched hard and found no way to do with timezones, I gathered what I found and put together an alternative... But tell me, do you think using timezones makes sense [semantically]? For me a Timezone was supposed to be a standardized thing, maybe that’s why there’s no easy way to create arbitrary timezones (including varying the minute). Take a look on that list to see what is currently supported (don’t forget to click on the links to the complete lists).

1


  • That’s the kind of answer I’ve been waiting for. Thank you.

Browser other questions tagged

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