Add custom timezones in PHP?

10

TL; DR

Is there any way to add custom timezones in PHP?

Objective

I would like to be able to record a timezone (using new DateTime(...) ) with an arbitrary timezone value, such as 13 minutes , 51 minutes , or 3 hours and 27 minutes , to be able to use the DateTime::diff() method to calculate the time interval between two events recorded by two distinct clocks may be out of sync with each other by an arbitrary (but known) time difference.

Practical example:

Event 1 : 17:40 (clocked 1)

Event 2 : 7:40 PM (logged by clock 2, which is 4 minutes late in relation to clock 1)

Time interval between events : 2 hours and 4 minutes.

    
asked by anonymous 12.01.2014 / 07:06

3 answers

1

From what I've read from Dercik Rethans , which developed much of the class DateTime , by 2010 create custom timezone 'was not even considered by him . Joining this to the current DateTimeZone documentation , I understand that it is not yet possible to create a specific timezone (for pattern). However, the answers to mgibsonbr and Sergio are alternatives.

    
17.01.2014 / 02:10
6

Maybe what you could use, other than the possibility that mgibsonbr indicated, is modify :

So you could change the DateTime object to give you the modified date with the difference entered.

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

    
12.01.2014 / 11:43
3

I do not know of any way to create custom% custom, but I can suggest an alternative using timestamps.

  • Convert your date to a timestamp ( timezones ), via int ( if your date is in the format strtotime ) or string (if it is in this format) :

    $timestamp1 = strtotime($hora1);
    $timestamp2 = $hora2->getTimestamp();
    
  • Adjust the difference in seconds for 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);
    
  • Convert them back to DateTime::getTimestamp , using DateTime :

    $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. Complete 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 the dates are in different timezones, you may need to first convert them to UTC before comparing them:

    function para_utc($timestamp)
    {
        return $timestamp - date("Z", $timestamp);
    }
    
        
    12.01.2014 / 08:11