php 时间选择,PHP-在学说2中的日期之间选择条目
PHP-在學(xué)說2中的日期之間選擇條目
我將因這個無法修復(fù)的最小錯誤而發(fā)瘋。 我想在兩天之間選擇條目,下面的示例說明了我所有的失敗:
選擇1。
$qb->where('e.fecha > ' . $monday->format('Y-m-d'));
$qb->andWhere('e.fecha < ' . $sunday->format('Y-m-d'));
結(jié)果(0個條目):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
FROM reservacion r0_
WHERE (r0_.fecha > 2012 - 07 - 16) AND (r0_.fecha < 2012 - 07 - 22)
選擇2
$qb->add('where', 'e.fecha between 2012-01-01 and 2012-10-10');
結(jié)果(0個條目):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
FROM reservacion r0_ WHERE r0_.fecha
BETWEEN 2012 - 01 - 01 AND 2012 - 10 - 10
這是我?guī)в挟?dāng)前條目的表:
id fecha cliente
1 2012-07-16 00:00:00 2
2 2012-07-16 13:00:00 4
3 2012-07-22 23:00:00 4
編輯1
為了評估sql以免產(chǎn)生疑問,我運(yùn)行了以下查詢:
$qb->where('e.fecha > ' . $sunday->format('Y-m-d'));
結(jié)果(3個條目):
SELECT r0_.id_reservacion AS id_reservacion0, r0_.fecha AS fecha1, r0_.cliente AS cliente2
因此,看起來不是sql的問題。???? 從保留r0_???? 在r0_.fecha> 2012-07
4個解決方案
137 votes
您可以…
$qb->where('e.fecha BETWEEN :monday AND :sunday')
->setParameter('monday', $monday->format('Y-m-d'))
->setParameter('sunday', $sunday->format('Y-m-d'));
要么…
$qb->where('e.fecha > :monday')
->andWhere('e.fecha < :sunday')
->setParameter('monday', $monday->format('Y-m-d'))
->setParameter('sunday', $sunday->format('Y-m-d'));
MacDada answered 2020-02-06T14:24:25Z
32 votes
我相信這樣做的正確方法是使用查詢生成器表達(dá)式:
$now = new DateTime();
$thirtyDaysAgo = $now->sub(new \DateInterval("P30D"));
$qb->select('e')
->from('Entity','e')
->add('where', $qb->expr()->between(
'e.datefield',
':from',
':to'
)
)
->setParameters(array('from' => $thirtyDaysAgo, 'to' => $now));
[http://docs.doctrine-project.org/en/latest/reference/query-builder.html#the-expr-class]
編輯:此方法相對于此處任何其他答案的優(yōu)點(diǎn)是它獨(dú)立于數(shù)據(jù)庫軟件-您應(yīng)該讓Doctrine處理日期類型,因?yàn)樗哂刑幚泶祟悊栴}的抽象層。
例如,如果執(zhí)行類似以'Y-m-d'形式添加字符串變量的操作,則該變量在轉(zhuǎn)到MySQL以外的數(shù)據(jù)庫平臺時會中斷。
Harry Mustoe-Playfair answered 2020-02-06T14:25:00Z
3 votes
編輯:請參閱其他答案以獲得更好的解決方案
我提供的原始新手方法是(opt1):
$qb->where("e.fecha > '" . $monday->format('Y-m-d') . "'");
$qb->andWhere("e.fecha < '" . $sunday->format('Y-m-d') . "'");
和(opt2):
$qb->add('where', "e.fecha between '2012-01-01' and '2012-10-10'");
這樣既快捷又容易,使原始海報立即生效。
因此,可以接受的答案。
根據(jù)評論,這是一個錯誤的答案,但這是一個容易犯的錯誤,因此,我將其作為“不可以做的事”留在這里。
azhrei answered 2020-02-06T14:25:42Z
1 votes
看看如何在參數(shù)中格式化日期$ jour。這取決于您使用的是expr()-> like還是expr()-> lte
$qb
->select('e')
->from('LdbPlanningBundle:EventEntity', 'e')
->where(
$qb->expr()->andX(
$qb->expr()->orX(
$qb->expr()->like('e.start', ':jour1'),
$qb->expr()->like('e.end', ':jour1'),
$qb->expr()->andX(
$qb->expr()->lte('e.start', ':jour2'),
$qb->expr()->gte('e.end', ':jour2')
)
),
$qb->expr()->eq('e.user', ':user')
)
)
->andWhere('e.user = :user ')
->setParameter('user', $user)
->setParameter('jour1', '%'.$jour->format('Y-m-d').'%')
->setParameter('jour2', $jour->format('Y-m-d'))
->getQuery()
->getArrayResult()
;
Laurent Lolo answered 2020-02-06T14:26:02Z
總結(jié)
以上是生活随笔為你收集整理的php 时间选择,PHP-在学说2中的日期之间选择条目的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java实现回文验证_LeetCode
- 下一篇: php 微信小程序 循环 多选,微信小程