学说fockby月和年



(hi),我需要从(mm-yyyy)形成的日期中找到所有对象,问题是格式的日期,我无法比较dateTime对象和字符串。

无法将类型"字符串"类型的php值'03 -2019'转换为" dateTime"。预期以下类型之一:NULL,DATETIME

我向您展示我的代码:

/**
 * @Route("/month/{date}", name="monthly_resume", methods={"GET"})
 * @param RentReleaseRepository $rentReleaseRepository
 * @param $date
 * @return Response
 */
public function monthlyCalcul(RentReleaseRepository $rentReleaseRepository, $date)
{
    if (substr($date, 0, 7) === 'Janvier') {
        $date = str_replace('01-', 'Janvier ', $date);
    } elseif (substr($date, 0, 7) === 'Février') {
        $date = str_replace('02-', 'Février ', $date);
    } elseif (substr($date, 0, 4) === 'Mars') {
        $date = str_replace('03-', 'Mars ', $date);
    } elseif (substr($date, 0, 5) === 'Avril') {
        $date = str_replace('04-', 'Avril ', $date);
    } elseif (substr($date, 0, 3) === 'Mai') {
        $date = str_replace('05-', 'Mai ', $date);
    } elseif (substr($date, 0, 4) === 'Juin') {
        $date = str_replace('06-', 'Juin ', $date);
    } elseif (substr($date, 0, 7) === 'Juillet') {
        $date = str_replace('07-', 'Juillet ', $date);
    } elseif (substr($date, 0, 4) === 'Août') {
        $date = str_replace('08-', 'Août ', $date);
    } elseif (substr($date, 0, 9) === 'Septembre') {
        $date = str_replace('09-', 'Septembre ', $date);
    } elseif (substr($date, 0, 7) === 'Octobre') {
        $date = str_replace('10-', 'Octobre ', $date);
    } elseif (substr($date, 0, 7) === 'Novembre') {
        $date = str_replace('110-', 'Novembre ', $date);
    } elseif (substr($date, 0, 8) === 'Décembre') {
        $date = str_replace('12-', 'Décembre ', $date);
    } else {
        throw new LogicException('OK ... So ... There is a problem');
    }
    $currentDate = new DateTime();
    $currentDate = $currentDate->format('m-Y');
    $rentRelease = $rentReleaseRepository->findBy(
        [
            'userRentRelease' => $this->getUser(),
            'date' => $currentDate, // the problem is here
        ]
    );
    //TODO calcul algorithm
    return $this->render('resume_page/month.html.twig', [
        'date' => $date
    ]);
}

创建我的日期,我做到了:

$rentRelease->setDate(new DateTime());

当然,这个问题有一个问题,但是我对如何解决它没有任何想法...您的帮助非常感谢:)

$currentDate = $currentDate->format('m-Y');更改为 $currentDate = $currentDate->format("Y-m") . "-01 00:00:00";

但是您必须定义逻辑代码,例如

    // use $year and $mounth as parameter !
    if (!is_numeric($year)) $year = date('Y');
    if (!is_numeric($mounth)) $mounth = date('m');
    $currentDate = DateTime::createFromFormat("Y-m-d", $year . "-" . $mounth.'-15');
    $start_mounth = $currentDate->format("Y-m") . "-01 00:00:00";
    $end_mounth = $currentDate;
    $end_mounth->add(new DateInterval('P01M'));
    $end_mounth = $end_mounth->format("Y-m") . "-01 00:00:00";

,在您的逻辑SQL中,您定义

    ->andWhere('e.date BETWEEN :start AND :end')
    ->setParameter('start', $start_mounth )
    ->setParameter('end', $end_mounth)

您可以尝试更改此信息:

 $rentRelease = $rentReleaseRepository->findBy(
    [
        'userRentRelease' => $this->getUser(),
        'date' => $currentDate, // the problem is here
    ]
);

到这个

$startDate = new DateTime('01-'.$date.' 00:00:00');
$endDate = (clone $startDate)->modify('+ 1 month - 1 second');
$rentRelease = $rentReleaseRepository->createQueryBuilder('rent')
    ->where('userRentRelease = :user')
    ->andWhere('date BETWEEN :dateFrom AND :dateTo')
    ->setParameters([
        'user' => $this->getUser(),
        'dateFrom' => $startDate,
        'dateTo' => $endDate,
    ])
    ->getQuery()
    ->getResult();

相关内容

  • 没有找到相关文章

最新更新