1

The code below gets the timezone offset of any city, but i want to trigger an action if the "TimeZoneid" ( The City ) is wrong or not supported at all by PHP

<?php

$dtz          = new DateTimeZone('Europe/Berlin');
$time_in_city = new DateTime('now', $dtz);
$TimeZone     =  $dtz->getOffset( $time_in_city );

if(empty($TimeZone)){
    echo "Timezoneid is wrong or not Supported";
}

else{
    echo "Timezoneid offset is $TimeZone";
}

?>

When I try to write a wrong TimeZoneid to test my code like this The code does not return anything

$dtz = new DateTimeZone('Europe/Blablabla');

but I want the code to tell me if the city is wrong or not supported.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Rayan Adams
  • 73
  • 1
  • 7
  • 1
    [DateTimeZone::listIdentifiers](https://www.php.net/manual/en/datetimezone.listidentifiers.php) returns a list of all supported values you can validate against. If you pass anything else there's probably an exception. Make sure you have error reporting and/or error logging turned on if you don't see it. – Peter Mar 31 '19 at 20:21

1 Answers1

0

Try this code:
Used the try catch for testing this application

<?php
try{
    $dtz =          new DateTimeZone('Europe/Blablabla');
    $time_in_city = new DateTime('now', $dtz);
    $TimeZone     =  $dtz->getOffset( $time_in_city );
    echo "Timezoneid offset is $TimeZone";

}
catch(Exception $e){
    echo "Timezoneid is wrong or not Supported";
}
?>
MARI Mathieu
  • 39
  • 1
  • 8