如何检查PHP中失败的DateTime? [英] How to check for a failed DateTime in PHP?

查看:156
本文介绍了如何检查PHP中失败的DateTime?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当以下数据发生坏数据时,PHP会中止。

When the following gets bad data PHP aborts.

PHP Fatal error:  Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (980671) at position 4 (7): Unexpected character'

如果数据不好采取其他行动,所以PHP问题不会失败?

How can I catch this if the data is bad to take other action so the PHP problem doesn't fail?

$date = new DateTime($TRANSACTION_DATE_MMDDYY_raw);


推荐答案

使用 try catch

try {
  $date = new DateTime($date);
} catch(Exception $e) {
  echo "Invalid date... {$e->getMessage()}";
}

这篇关于如何检查PHP中失败的DateTime?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆