>  기사  >  백엔드 개발  >  Symfony2 쿼리 입력 시간에 대한 자세한 설명

Symfony2 쿼리 입력 시간에 대한 자세한 설명

零下一度
零下一度원래의
2017-07-02 11:55:511032검색

이 글에서는 주로 Symfony2의 입력 시간 조회 방법을 소개하고, mysql과 MongoDB의 입력 시간을 변환 및 조회하는 Symfony2의 관련 연산 기술을 예제 형식으로 분석합니다. 이 글

Symfony2에서 입력 시간을 조회하는 방법을 설명합니다. 참조를 위해 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

일반적으로: 프런트 엔드가 시간을 입력할 때 일반적으로 먼저 시간을 timestamp

strtotime으로 수정합니다. - 날짜를 설명하고 time of any English text Parse to Unix timestamp

예:


$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);

Then: 단지 시간이라면 다른 사람이 지나간 시간이 위조되는 것을 방지하기 위해 시간을 다음 형식으로 수정해야 합니다. of 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 유형으로 식별하는 데 도움을 주지 않습니다. 어떤 유형인지 수동으로 전달해야 합니다.

rreee

위 내용은 Symfony2 쿼리 입력 시간에 대한 자세한 설명의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.