本文实例讲述了Symfony2针对输入时间进行查询的方法。分享给大家供大家参考,具体如下:
一般情况下:前端输入一个时间,我们一般是先将时间修改成一个时间戳
strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳
例如:
$startTimestamp = strtotime($startDate); $endTimestamp = strtotime($endDate);
然后:如果只是时间,为防止别人传的时间是造假,需要将时间都修改成Y-m-d的形式
$start = date('Y-m-d 00:00:00', $startTimestamp); $end = date('Y-m-d 23:59:59', $endTimestamp);
1. 在MySQL中的使用
if(empty($startDate)) { throw new \Exception('起始时间不为空', BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } if (empty($endDate)) { $endDate = $startDate; } $startTimestamp = strtotime($startDate); $endTimestamp = strtotime($endDate); if ($startTimestamp > $endTimestamp) { throw new \Exception('时间参数错误', BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } if ($status == InventoryOrder::STATUS_SUBMITTED) { $index = 'i.submitTime'; } else if ($status == InventoryOrder::STATUS_UNSUBMITTED) { $index = 'i.createTime'; } else if (empty($status)) { $index = 'i.createTime'; } else { throw new \Exception('时间格式不正确', BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } $sql = 'SELECT i FROM AppBundle:InventoryOrder i WHERE '; $sql .= $index; $sql .= ' BETWEEN :startDate AND :endDate '; $start = date('Y-m-d 00:00:00', $startTimestamp); $end = date('Y-m-d 23:59:59', $endTimestamp); $params['endDate'] = $end; $params['startDate'] = $start; if (!empty($status)) { $sql .= ' AND i.status = :status'; $params['status'] = $status; } $sql .=' ORDER By i.createTime DESC'; $query = $this->entityManager->createQuery($sql); $orderList = $query->setParameters($params)->getResult();
2. 在mongodb中的时间的输入和查询列子
在这里面其实有两个坑:
@ ->field('submit_time')->gt(new \DateTime($start)) ->field('submit_time')->lt(new \DateTime($end))
这里面,对于时间的查询,大于和小于,一定要传一个对象。
$query->field('status')->equals($status);
这里面,在mongodb里面不会默认帮你识别这是一个int型,是什么类型,必须手动的传入。
$data = array(); if (!isset($startDate)) { throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } if (empty($endDate)) { $endDate = $startDate; } $startTimestamp = strtotime($startDate); $endTimestamp = strtotime($endDate); if ($startTimestamp > $endTimestamp) { throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } $start = date('Y-m-d 00:00:00', $startTimestamp); $end = date('Y-m-d 23:59:59', $endTimestamp); $scanner = Order::FROM_TYPE_SCANNER; $query = $this->documentManager ->createQueryBuilder('AppBundle:Order') ->field('submit_time')->gt(new \DateTime($start)) ->field('submit_time')->lt(new \DateTime($end)) ->field('from_type')->equals("$scanner"); if (!empty($status) && in_array($status, array(Order::STATUS_CANCELLED, Order::STATUS_SUBMITTED))) { $status = $status + 0; $query->field('status')->equals($status); } else if (empty($status)) { $status = Order::STATUS_SUBMITTED + 0; $query->field('status')->equals($status); } else { throw new \Exception("参数不正确", BaseException::ERROR_CODE_ILLEGAL_PARAMETER); } $orderList = $query->sort('create_time', 'DESC') ->getQuery() ->execute();
更多关于Symfony2相关内容感兴趣的读者可查看本站专题:《Symfony框架入门教程》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《php优秀开发框架总结》、《ThinkPHP入门教程》、《ThinkPHP常用方法总结》、《Zend FrameWork框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于Symfony2框架的PHP程序设计有所帮助。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年09月23日
2024年09月23日
- 庄锭欣 ELKIE《没有你的未来》[320K/MP3][15.78MB]
- 庄锭欣 ELKIE《没有你的未来》[FLAC/分轨][36MB]
- TchaikovskySwanLake,Op.20柴可夫斯基天鹅湖(2013)SACD[Hi-Res]
- 24bit达人艺典《琴歌终极版》整轨WAV+CUE
- 黑鸭子.2005-《发烧DSD》柏菲[WAV分轨]
- 群星.1990-电影看板·人间道【宝丽金】【WAV+CUE】
- 周殷廷.2024-TO.WHOM.IT.MAY.CONCERN【环球】【FLAC分轨】
- 草蜢.1997-三人世界精选17首【宝丽金】【WAV+CUE】
- 【古琴音乐】管平湖《广陵散》2004[FLAC+CUE]
- 【中国民乐】《少数民族器乐·苗岭的早晨》2004[WAV+CUE]
- 【中国音乐】盛中国《新疆之春》2004[FLAC+CUE]
- 群星《音你而来 第8期》[320K/MP3][44.64MB]
- 群星《音你而来 第8期》[FLAC/分轨][121.21MB]
- 群星《十二女声Ⅲ》[WAV/CUE/分轨][626.4MB]
- 陈百强.1993-离不开...纪念歌集2CD【华纳】【WAV+CUE】