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 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable