Symfony2針對輸入時間進行查詢的方法分析
本文實例講述了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('時間參數(shù)錯誤', 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("參數(shù)不正確", BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
if (empty($endDate)) {
$endDate = $startDate;
}
$startTimestamp = strtotime($startDate);
$endTimestamp = strtotime($endDate);
if ($startTimestamp > $endTimestamp) {
throw new \Exception("參數(shù)不正確", 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("參數(shù)不正確", BaseException::ERROR_CODE_ILLEGAL_PARAMETER);
}
$orderList = $query->sort('create_time', 'DESC')
->getQuery()
->execute();
更多關于Symfony2相關內(nèi)容感興趣的讀者可查看本站專題:《Symfony框架入門教程》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《php優(yōu)秀開發(fā)框架總結》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Symfony2框架的PHP程序設計有所幫助。
- Symfony查詢方法實例小結
- Symfony2聯(lián)合查詢實現(xiàn)方法
- Symfony2使用Doctrine進行數(shù)據(jù)庫查詢方法實例總結
- 高性能PHP框架Symfony2經(jīng)典入門教程
- Symfony2實現(xiàn)從數(shù)據(jù)庫獲取數(shù)據(jù)的方法小結
- Symfony2學習筆記之模板用法詳解
- Symfony2框架學習筆記之表單用法詳解
- Symfony2框架創(chuàng)建項目與模板設置實例詳解
- symfony2.4的twig中date用法分析
- Symfony2學習筆記之控制器用法詳解
- Symfony2安裝第三方Bundles實例詳解
- Symfony2實現(xiàn)在controller中獲取url的方法
相關文章
基于Laravel實現(xiàn)的用戶動態(tài)模塊開發(fā)
這篇文章主要給大家介紹了關于基于Laravel實現(xiàn)的用戶動態(tài)模塊開發(fā)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-09-09
laravel 如何實現(xiàn)引入自己的函數(shù)或類庫
laravel 如何實現(xiàn)引入自己的函數(shù)或類庫?今天小編就為大家解答一下在laravel中引入自己的函數(shù)或類庫的方法,分享給大家,給大家做個參考,一起過來看看吧2019-10-10
詳解在YII2框架中使用UEditor編輯器發(fā)布文章
這篇文章主要介紹了在YII2框架中使用UEditor編輯器發(fā)布文章,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11
Yii 實現(xiàn)數(shù)據(jù)加密和解密的示例代碼
這篇文章主要介紹了Yii 實現(xiàn)數(shù)據(jù)加密和解密的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-01-01
使用gd庫實現(xiàn)php服務端圖片裁剪和生成縮略圖功能分享
一般用戶上傳頭像時,都會讓用戶自行裁剪圖片。那么php怎么實現(xiàn)這個功能呢?php中裁剪圖片主要使用gd庫的imagecopyresampled方法2013-12-12
php函數(shù)mkdir實現(xiàn)遞歸創(chuàng)建層級目錄
當我們想在自己的站點中添加多級目錄時,可以運用php函數(shù)mkdir()來實現(xiàn)這個功能。具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-10-10

