php显示当月每天,php 怎么获取当前月份往前24个月中的每个月的订单总和?

直接上代码咯,看着代码和注释能清晰一点。

$orders = array(); // 储存每个月的订单

$current_year = (int) date('Y-m', time()); // 当前年份

$current_month = (int) date('Y-m', time()); // 当前月份

for ($i = 1; $i < 25; $i++) { // 循环24次

// 获取本月最后一天24点的时间戳(为什么这样获取呢?看示例,例如:1月31日24点 = 2月1日0点)

$timestamp_end = strtotime($current_year . $current_month); // 注意current_year和month都是上次循环的

// 本次循环要获取订单数的月份

$current_month = $current_month - $i;

if ($current_month === 0) { // 月份减一等于零,证明是要从1月跳到上一年的12月

$current_month = 12; // 本次要获取订单数的月份

$current_year = $current_year - 1; // 本次要获取订单数的年份

}

// 获取本月第一天0点的时间戳

$timestamp_start = strtotime($current_year . $current_month); // 获取这个月刚开始时的时间戳

// 根据时间戳获取订单(传入的时间戳区间就是这个月的)

$sql = "SELECT * FROM orders WHERE create_time >= $start_timestamp AND create_time <= $end_timestamp";

// 后面查这段sql就可以获取当月订单了,(假设这个query()是执行sql的方法)

$orders[$current_year][$current_month] = query($sql);

}

哈哈,希望对你有帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值