date_offset_get


php apg

RETURNS the time zone offset in seconds from UTC.



<?php

/* - - - - - - - - - - - - - - - - - - -

    Object oriented style I
   
   - - - - - - - - - - - - - - - - - - - */

int public DateTime::getOffset ( )

?>

<?php

/* - - - - - - - - - - - - - - - - - - - -

    Object oriented style II
   
   - - - - - - - - - - - - - - - - - - - - */

int public DateTimeImmutable::getOffset ( )

?>

<?php

/* - - - - - - - - - - - - - - - - - - - -

    Object oriented style III
   
   - - - - - - - - - - - - - - - - - - - - */

int public DateTimeInterface::getOffset ( )

?>

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

int date_offset_get ( DateTimeInterface $object )


where,


$object = DateTimeZone object representing the desired time zone

?>

 $object 


The DateTime object representing the desired TimeZone.





For Procedural style, only:
A DateTime object returned by the function date_create.

Prior to PHP 8.0.0 this function returns FALSE on failure.



  1 EXERCISE   

<?php

$tz01 
= 'America/Sao_Paulo';

$asp01 = new DateTime('now', new DateTimeZone($tz01));

echo 
'offset = ' . $asp01->getOffset() . 's = ' . bcdiv($asp01->getOffset(), 3600). 'h<br><br>for ' . $tz01 . ' in relation to UTC.<br>';

?>

  2 EXERCISE   

<?php

$tz02 
= 'UTC';

$asp02 = new DateTimeImmutable('now', new DateTimeZone($tz02));

echo 
'offset = ' . $asp02->getOffset() . 's = ' . bcdiv($asp02->getOffset(), 3600). 'h<br><br>for ' . $tz02 . ' in relation to UTC.<br>';

?>

  3 EXERCISE   

<?php

// With each new run, a new offset 
// will be displayed for a new time zone

$arrtz03 = timezone_identifiers_list(); 
$cnt03 = (count($arrtz03) - 1); 

$rd03 = mt_rand(0, $cnt03); 
$tz03  = $arrtz03[$rd03]; 

date_default_timezone_set($tz03); 

$asp03 = date_create ("now", timezone_open($tz03));

$ofs03 = date_offset_get($asp03);

echo 
'offset = ' . $ofs03 . 's = ' . bcdiv($ofs03, 3600). 'h<br><br>for ' . $tz03 . ' in relation to UTC.<br>';

?>