Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中检查日期范围时出现问题_Php_Symfony - Fatal编程技术网

在PHP中检查日期范围时出现问题

在PHP中检查日期范围时出现问题,php,symfony,Php,Symfony,正如标题所示,我的代码中的日期范围检查有一个奇怪的问题。这是: 首先,AJAX查询进入ReceiptQueryController.php,它检查请求,并检查给定日期是否在一个范围内 /** * @Route("/querysearch", name="ReceiptQuerySearch") */ public function querySearchAction() { $request = $this->container-

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

首先,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;
    }

}

几乎两天后就没有答案了

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

尝试使用
格式

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));
}
函数检查范围($fromDate、$toDate、$userDate)
{
$sDate=日期('Y-m-d',$fromDate);
$eDate=日期('Y-m-d',$toDate);
$uDate=$userDate->format('Y-m-d');

return(($uDate>=$sDate)&&($uDate这是一条注释而不是答案,但是注释部分有一些代码相对不可读

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

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

// 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();
//获取存储库
$receiptRepository=$this->getDoctrine()->getRepository('AdminStoreReceiptReceiptBundle:Receipt');
//使用“r”作为表的别名创建查询生成器
$receiptQueryBuilder=$receiptRepo->createQueryBuilder('r');
//查找r.date>=$fromDate&&r.date其中($receiptQueryBuilder->expr()->介于('r.date',':from',':to')之间的所有收据
->setParameter('from',$fromDate)
->setParameter('to',$toDate)
->getQuery()
->getResults();
这将为您提供大于等于
$fromDate
且小于等于
$toDate
的所有收据

然后将该调用移动到自定义存储库中,这样您就可以在任何地方使用它,比如
$receiptList=$receiptRepository->findallbetween-dates($fromDate,$toDate)

您保存了我的一天:)tnx
// 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();