Home Backend Development PHP Tutorial Detailed explanation of Symfony2 input time for query

Detailed explanation of Symfony2 input time for query

Jul 02, 2017 am 11:55 AM
Inquire conduct

This article mainly introduces the method of Symfony2 querying the input time, and analyzes the relevant operation skills of Symfony2 for converting and querying the input time of mysql and MongoDB in combination with examples. Friends who need it can Refer to the following

The example of this article describes the method of querying the input time in Symfony2. Share it with everyone for your reference, the details are as follows:

Under normal circumstances: when the front end inputs a time, we usually modify the time to a timestamp

strtotime — Parse any English text datetime description into a Unix timestamp

For example:


$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
Copy after login

then : If it is just time, in order to prevent the time passed by others from being falsified, you need to modify the time into the form of Y-m-d


$start = date('Y-m-d 00:00:00', $startTimestamp);
$end = date('Y-m-d 23:59:59', $endTimestamp);
Copy after login

1. Use in 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();
Copy after login

2. Time input and query examples in mongodb

There are actually two pitfalls in this :


@ ->field('submit_time')->gt(new \DateTime($start))
->field('submit_time')->lt(new \DateTime($end))
Copy after login

Here, for time query, greater than and less than, an object must be passed.


$query->field('status')->equals($status);
Copy after login

Here, mongodb will not help you identify this as an int type by default. What type it is, it must be passed in manually.


$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();
Copy after login

The above is the detailed content of Detailed explanation of Symfony2 input time for query. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check your academic qualifications on Xuexin.com

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

12306 How to check historical ticket purchase records How to check historical ticket purchase records

How to check the activation date on Apple mobile phone How to check the activation date on Apple mobile phone Mar 08, 2024 pm 04:07 PM

How to check the activation date on Apple mobile phone

How to use Oracle to query whether a table is locked? How to use Oracle to query whether a table is locked? Mar 06, 2024 am 11:54 AM

How to use Oracle to query whether a table is locked?

Comparison of similarities and differences between MySQL and PL/SQL Comparison of similarities and differences between MySQL and PL/SQL Mar 16, 2024 am 11:15 AM

Comparison of similarities and differences between MySQL and PL/SQL

How to check the latest price of Tongshen Coin? How to check the latest price of Tongshen Coin? Mar 21, 2024 pm 02:46 PM

How to check the latest price of Tongshen Coin?

How to check the latest price of INJ coin? How to check the latest price of INJ coin? Mar 06, 2024 pm 05:00 PM

How to check the latest price of INJ coin?

Discuz database location query skills sharing Discuz database location query skills sharing Mar 10, 2024 pm 01:36 PM

Discuz database location query skills sharing

See all articles