PHP tp5.1 根据当天,本月,本季度的时间戳查询数据

这篇博客介绍了如何在PHP TP5.1框架下,利用时间戳来查询当天、本月及本季度的数据。通过获取时间戳,设置查询条件,进行数据库查询,并返回结果。适合初学者了解PHP时间查询操作,文章中提到时间戳的获取参考了其他博主的代码链接,并鼓励大家提出意见和讨论。

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

千里之行,始于足下,希望日积月累。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值