我想在一个日期间隔内显示我的所有订单,该订单在URL:localhost/ws/v1/orders/2019-01-01/2019-01-31 上发送
但无论我插入什么日期,我的结果总是我的第一个订单。如果我键入一个没有2个日期的URL,不要返回任何内容。
控制器
public function getOrders_get() {
header("Access-Control-Allow-Origin: *");
// Load Authorization Token Library
$this->load->library('Authorization_Token');
$fromDate = $this->uri->segment(3);
$toDate = $this->uri->segment(4);
/**
* User Token Validation
*/
$is_valid_token = $this->authorization_token->validateToken();
if (!empty($is_valid_token) AND $is_valid_token['status'] === TRUE)
{
if (empty($id)){
$data = $this->db->get_where("`ga845_pedidos_view` WHERE `data` BETWEEN date('$fromDate') AND date('$toDate')")->row_array();
}else{
$data = $this->db->get_where("ga845_pedidos_view", ['id' => $id])->row_array();
}
$this->response($data, REST_Controller::HTTP_OK);
} else {
$this->response(['status' => FALSE, 'message' => $is_valid_token['message'] ], REST_Controller::HTTP_NOT_FOUND);
}
}
返回:
"id": "160",
"data": "2017-01-25 12:33:24",
"status": "3",
...
I用这种方式解决这个问题。
我的URL:localhost/ws/v1/2019-01-01/2019-01-101st-本地主机;第2-ws;3rd-v1;2019年1月4日;第5期-2019-01-10
$fromDate = $this->uri->segment(4); // 4 step of URL
$toDate = $this->uri->segment(5); // 5 step of URL
$this->db->where('order_date >=', $fromDate);
$this->db->where('order_date <=', $toDate);
$data = $this->db->get("order_table")->result();
您必须在strtotime((中转换日期并像一样进行检查
$from = strtotime($fromDate);
$to = strtotime(toDate);
您的查询是
BETWEEN {$from} AND {$to}