繁体   English   中英

无法检查PHP中的日期范围

[英]Trouble checking the date range in PHP

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

首先,AJAX查询进入ReceiptQueryController.php ,它检查请求,并检查给定的日期是否在range之间

    /**
     * @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));
    }

将近2天没有答案!

在您的方法中,$ userdate是一个对象。 您不能将其作为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)等任何地方使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM