1获取时间戳
当天时间戳:
//这里是当天时间戳
$start_today=strtotime(date("Y-m-d",time()));
$end_today=strtotime(date('Y-m-d',time()))+86400-1;
//本月时间戳
$start_month=strtotime(date('Y-m-01'));
$end_month=strtotime(date('Y-m-d',strtotime('+1 day')));
//本季度时间戳
$quarter = empty($param) ? ceil((date('n'))/3) : $param;//获取当前季度
$start_quarter = mktime(0, 0, 0,$quarter*3-2,1,date('Y'));
$end_quarter = mktime(0, 0, 0,$quarter*3+1,1,date('Y'));
2查询条件
$wheretoday=[
['time','>=',$start_today],
['time','<=',$end_today],
];
$wheremonth=[
['time','>=',$start_month],
['time','<=',$end_month],
];
$wherequarter =[
['time','>=',$start_quarter],
['time','<=',$end_quarter],
];
3查询数据
$today = Db::name('account')->where($wheretoday)->select(); //当天的数据
$month = Db::name('account')->where($wheremonth)->select(); //本月的数据
$quarter = Db::name('account')->where($wherequarter)->select(); //本季度的数据
4 返回结果
//这里是json数据返回格式
return json([
'code' => 200,
'msg' => '数据查询成功',
'today' =>$today,
"month"=>$month,
'quarter' => $quarter,
]);
最后总结
可用于一般的时间查询。
初入php, 有很多不懂的地方见谅,希望多提意见一起探讨。
时间戳是参考大佬的代码
这里带上地址:https://blog.youkuaiyun.com/qq_22823581/article/details/88233240
千里之行,始于足下,希望日积月累。
这篇博客介绍了如何在PHP TP5.1框架下,利用时间戳来查询当天、本月及本季度的数据。通过获取时间戳,设置查询条件,进行数据库查询,并返回结果。适合初学者了解PHP时间查询操作,文章中提到时间戳的获取参考了其他博主的代码链接,并鼓励大家提出意见和讨论。
2269

被折叠的 条评论
为什么被折叠?



