(嗨), 我需要从格式为(mm-yyyy)的日期中查找所有对象,问题是格式日期为字符串,并且我无法比较dateTime对象和字符串。
无法将类型为“字符串”的PHP值“ 03-2019”转换为类型“日期时间”。预期为以下类型之一: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());
这个问题当然有问题,但是我不知道如何解决...感谢您的帮助:)
答案 0 :(得分: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();
答案 1 :(得分: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)