dtc88867 2019-03-31 19:46
浏览 58
已采纳

如果条目(DateTimeZone)错误或为空,则触发操作

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.

  • 写回答

1条回答 默认 最新

  • dp5657087 2019-03-31 22:14
    关注

    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";
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题