本文实例讲述了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();
更多关于symfony2相关内容感兴趣的读者可查看本站专题:《symfony框架入门教程》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《php优秀开发框架总结》、《thinkphp入门教程》、《thinkphp常用方法总结》、《zend framework框架入门教程》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家基于symfony2框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!