• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

Symfony2针对输入时间进行查询实例详解

php 搞代码 3年前 (2022-01-22) 27次浏览 已收录 0个评论

一般情况下:前端输入一个时间,我们一般是先将时间修改成一个时间戳strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳本文主要和大家介绍了Symfony2针对输入时间进行查询的方法,结合实例形式分析了Symfony2针对mysql及MongoDB的输入时间进行转换与查询的相关操作技巧,需要的朋友可以参考下,希望能帮助到大家。

例如:

$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<em>/本2文来源[email protected]搞@^&代*@码2网</em><strong>搞gaodaima代码</strong>::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框架表单的用法

详解Symfony2实现联合查询的方法

Symfony2输入时间进行查询的详解

以上就是Symfony2针对输入时间进行查询实例详解的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:Symfony2针对输入时间进行查询实例详解

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址