在PHP中检查日期范围时出错


Trouble checking the date range in PHP

正如标题所示,我的代码中的日期范围检查存在一个奇怪的问题。这是:

首先,AJAX查询到达ReceiptQueryController.php,它检查请求,并检查给定日期是否在范围之间。

    /**
     * @Route("/querysearch", name="ReceiptQuerySearch")
     */
    public function querySearchAction()
    {
        $request = $this->container->get('request');
        $fromDate = date('Y-m-d', strtotime($request->request->get('fd')));
        $toDate = date('Y-m-d', strtotime($request->request->get('td')));
        $response = new Response();
        $response->headers->set('Content-Type', 'application/json');
        $em = $this->getDoctrine()->getRepository('AdminStoreReceiptReceiptBundle:Receipt');
        $em_product = $this->getDoctrine()->getRepository('AdminStoreProductProductBundle:Product');
        $receiptList = array();
        foreach ($em->findAll() as $entity) {
            if ($this->check_in_range($fromDate, $toDate, $entity->getDate())) {
                $receiptDetail = array();
                $receiptDetail['date'] = $entity->getDate()->format('Y/m/d');
                $receiptDetail['time'] = $entity->getTime()->format('H:i');
                $strRes = "";
                $totalPrice = 0;
                $totalPPrice = 0;
                foreach ($entity->getObjects() as $obj) {
                    $product = $em_product->findOneBy(array('barCode' => $obj));
                    $strRes .= $product->getName() . "<span class='pull-right'>خ: " . $product->getPurchasePrice() . ", ف: " . $product->getSalesPrice() . "</span><br>";
                    $totalPrice += $product->getSalesPrice();
                    $totalPPrice += $product->getPurchasePrice();
                }
                $receiptDetail['objects'] = $strRes;
                $receiptDetail['totalReceiptPrice'] = $totalPrice;
                $receiptDetail['totalReceiptPPrice'] = $totalPPrice;
                $receiptList[] = $receiptDetail;
            }
        }
        if (sizeof($receiptList) == 0){
            $receiptList[] = "No Result!";
        }
        $response->setContent(json_encode($receiptList));
        return $response;
    }
}

那么,$this->check_in_range($fromDate, $toDate, $entity->getDate())函数似乎无法按预期正常工作!

function check_in_range($fromDate, $toDate, $userDate)
    {
        $sDate = date('Y-m-d', $fromDate);
        $eDate = date('Y-m-d', $toDate);
        $uDate = date('Y-m-d', $userDate);
        return (($uDate >= $sDate) && ($uDate <= $eDate));
    }

几乎两天后没有答案!

在您的方法中,$userdate是一个Object。不能将其作为date函数的第二个参数传递。

尝试使用format:

function check_in_range($fromDate, $toDate, $userDate)
{
    $sDate = date('Y-m-d', $fromDate);
    $eDate = date('Y-m-d', $toDate);
    $uDate = $userDate->format('Y-m-d');
    return (($uDate >= $sDate) && ($uDate <= $eDate));
}

这是一个注释,而不是一个答案,但在注释部分有一些代码相对不可读。

您应该将日期检查移到存储库调用中,而不是将所有内容都加载到内存中,因为随着应用程序的增长,这可能会变得无法管理。

要获取一段时间内的所有收据,您可以使用。。。

// Get the repository
$receiptRepository = $this->getDoctrine()->getRepository('AdminStoreReceiptReceiptBundle:Receipt');
// Create a query builder using "r"as the alias for the table
$receiptQueryBuilder = $receiptRepo->createQueryBuilder('r');
// Find all receipts where r.date >= $fromDate && r.date <= $toDate
$receiptList = $receiptQB
    ->where($receiptQueryBuilder->expr()->between('r.date', ':from', ':to'))
    ->setParameter('from', $fromDate)
    ->setParameter('to', $toDate)
    ->getQuery()
    ->getResults();

这将为您提供等于或大于$fromDate且等于或小于$toDate的所有收据。

然后,将该调用移动到自定义存储库中是有意义的,这样您就可以在类似$receiptList = $receiptRepository->findAllBetweenDates($fromDate, $toDate)的任何地方使用它。