本文实例讲述了Symfony2针对输入时间进行查询的方法。分享给大家供大家参考,具体如下:
一般情况下:前端输入一个时间,我们一般是先将时间修改成一个时间戳
strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳
例如:
?
|
1
2
|
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
|
然后:如果只是时间,为防止别人传的时间是造假,需要将时间都修改成Y-m-d的形式
?
|
1
2
|
$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
|
1. 在MySQL中的使用
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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();
|
在这里面其实有两个坑:
?
|
1
2
|
@ ->field('submit_time')->gt(new \\DateTime($start))
->field('submit_time')->lt(new \\DateTime($end))
|
?
|
1
|
$query->field('status')->equals($status);
|
这里面,在mongodb里面不会默认帮你识别这是一个int型,是什么类型,必须手动的传入。
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
$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框架的PHP程序设计有所帮助。
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 90
-
2025-06-04 32
-
2025-05-25 50
-
2025-05-25 31
-
2025-05-29 89
热门评论

