duansha7453 2015-03-22 07:25
浏览 63
已采纳

PHP将Datetime对象与当前日期时间进行比较并找到更小的

I am getting datetime from the user through a form and the datetime entered by him, should be greater than current instance of time to be valid.

I am trying something like this

$input_format = 'd M Y - h:i a';
$date = DateTime::createFromFormat($input_format, $_POST['delivery_time']);
$now = new DateTime();
if(!$date || ($date->diff($now) > 0))
{
   $error .= '* Invalid Delivery Day/Time';
   error_log('WARNING :: Invalid Delivery Time at LINE: '.__LINE__.' in '.__FILE__.'
',3,$logfile);
}

I am having problem in the comparison.I get Object of class DateInterval could not be converted to int as error. What is the correct method to ensure that the entered time is greater than correct instance of time

  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2015-03-22 07:39
    关注

    DateTime::diff returns a DateInterval which has the property invert:

    invert

    Is 1 if the interval represents a negative time period and 0 otherwise.


    So in place of $date->diff($now) > 0 you can use simply $now->diff($date)->invert.

    (Note: this isn't strictly "greater than", it's effectively "greater than or equal to". It's easy to flip it around to be so if that's important, but it didn't seem relevant enough to warrant it.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向