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.)

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

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?