教条找到了月和年

问题描述 投票:0回答:2

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

无法将'string'类型的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());

当然这个问题有一个问题,但我不知道如何解决它......你的帮助很感激:)

php database symfony doctrine-orm symfony4
2个回答
0
投票

$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)

0
投票

您可以尝试更改此内容:

 $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();
© www.soinside.com 2019 - 2024. All rights reserved.