thinkphp实现mysql查询接近过期的排在上面,依此类推:
$list = $this->model->field($field)->alias('o')
->join("{$prefix}product_type t",'o.product_type_id = t.id')->where('o.product_type_id',9)->where('o.gskc','=',0)->where('o.status','=',1)
// ->order('o.create_time desc')
// 重点使用DATEDIFF
->orderRaw('DATEDIFF(FROM_UNIXTIME(o.classes_endtime), NOW()) ASC')
->paginate(['list_rows'=>$pageSize,'page'=>$page])
->toArray();
DATEDIFF() 函数返回两个日期之间的天数。
如果需要精确到秒级别可以使用timestampdiff
Select timestampdiff(SECONDS, '2023-09-20 12:30:15', '2023-09-01 10:15:00') as Difference;
第一个参数是差值单位可以是:
FRAC_SECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.
相关文章