首页 > 解决方案 > 比较两个 DateTime 对象的 PHP 性能负载

问题描述

我有一个可用性类,它会进行许多检查以查看日期是否可用,但其中一项检查是查看可预订产品是否在预订窗口内(即将来足以满足用户当天预订的标准) . 代码如下 -

private function set_within_booking_window(){
    $timezone = get_option('timezone_string');
    $starttime = cb_get_product_starttime($this->product_id, $this->date);
    $this->starttime = $starttime;
    //get booking window
    $window = new DateTime();
    $window->setTimezone(new DateTimeZone($timezone));
    $buffer = get_option("cb_same_day_buffer");
    $window->add(new DateInterval("PT".$buffer."H"));
    $this->window = $window;
    if($starttime < $window){
      $this->within_booking_window = false;
    } else {
      $this->within_booking_window = true;
    }
  }

在性能研究中,除了 if($starttime < $window) 的 DateTime 比较之外,整个操作构建良好且速度很快,这基本上会杀死整个可用性类的性能。页面加载不到 1 秒,该表达式被注释掉,超过 26 秒。

我在 PHP 文档中读到的所有内容都表明这是比较两个日期时间的最佳方法 - 但这不可能是正确的......

比较两个 PHP DateTime 对象时有任何关于性能的反馈吗?

标签: phpperformancedatetimecomparison

解决方案


推荐阅读