Php 如果条目(DateTimeZone)错误或为空,则触发操作
下面的代码获取任何城市的时区偏移量,但是如果“TimeZoneid”(城市)错误或者PHP根本不支持,我想触发一个操作Php 如果条目(DateTimeZone)错误或为空,则触发操作,php,Php,下面的代码获取任何城市的时区偏移量,但是如果“TimeZoneid”(城市)错误或者PHP根本不支持,我想触发一个操作 <?php $dtz = new DateTimeZone('Europe/Berlin'); $time_in_city = new DateTime('now', $dtz); $TimeZone = $dtz->getOffset( $time_in_city ); if(empty($TimeZone)){ echo
<?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";
}
?>
但是我想让代码告诉我该城市是否错误或不受支持。请尝试以下代码:使用try-catch测试此应用程序
<?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";
}
?>
试试这个代码:使用try-catch测试此应用程序
<?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";
}
?>
返回可验证的所有支持值的列表。如果您传递了其他内容,则可能会出现异常。如果看不到,请确保已打开错误报告和/或错误日志记录。返回可根据其进行验证的所有支持值的列表。如果您传递了其他内容,则可能会出现异常。如果看不到,请确保已打开错误报告和/或错误日志记录。