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();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值