thinkphp:查询本周中每天中日期的数据,查询今年中每个月的数据,查询近五年每年的总数据

一、查询本周中每天中日期的数据

结果:

以今天2023-09-14为例,这一周为2023-09-11~2023-09-07

代码

后端thinkphp:

php 复制代码
//查询本周每天的的总金额数
//获取本周的起始日期和结束日期
$weekStart = date('Y-m-d', strtotime('this week Monday'));
$weekEnd = date('Y-m-d', strtotime('this week Sunday'));
// 构造日期范围数组(从周一到周天)
$dateRange = [];
$currentDate = $weekStart;
while ($currentDate <= $weekEnd) {
    $dateRange[] = $currentDate;
    $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
}
// 查询每天的总金额数
$result = Db::table('so_headers_all')
    ->field("DATE_FORMAT(FROM_UNIXTIME(creation_date), '%Y-%m-%d') AS date, IFNULL(SUM(order_all_amount), 0) AS total_amount")
    ->whereTime('creation_date', '>=', $weekStart)
    ->whereTime('creation_date', '<=', $weekEnd)
    ->group('date')
    ->select();
// 构造最终结果数组
$resultArray = [];
foreach ($dateRange as $date) {
    $found = false;
    foreach ($result as $row) {
        if ($row['date'] == $date) {
            $resultArray[] = $row;
            $found = true;
            break;
        }
    }
    if (!$found) {
        $resultArray[] = ['date' => $date, 'total_amount' => 0];
    }
}
$data['week_info'] = $resultArray;

扩展:增加星期

结果

代码

后端thinkphp:

php 复制代码
//查询本周每天的的总金额数
//获取本周的起始日期和结束日期
$weekStart = date('Y-m-d', strtotime('this week Monday'));
$weekEnd = date('Y-m-d', strtotime('this week Sunday'));
// 构造日期范围数组(从周一到周天)
$dateRange = [];
$currentDate = $weekStart;
while ($currentDate <= $weekEnd) {
    $dateRange[] = $currentDate;
    $currentDate = date('Y-m-d', strtotime($currentDate . ' +1 day'));
}
// 查询每天的总金额数
$result = Db::table('so_headers_all')
    ->field("DATE_FORMAT(FROM_UNIXTIME(creation_date), '%Y-%m-%d') AS date, IFNULL(SUM(order_all_amount), 0) AS total_amount")
    ->whereTime('creation_date', '>=', $weekStart)
    ->whereTime('creation_date', '<=', $weekEnd)
    ->group('date')
    ->select();
//去掉逗号,转换为
foreach ($result as &$item) {
    // $item['total_amount'] = intval(($item['total_amount'] / 10000));
    $item['total_amount'] = round(($item['total_amount'] / 10000),2);
}
// 构造最终结果数组
$resultArray = [];
$dateArray = [];
$totalAmountArray = [];
$weekdays = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

foreach ($dateRange as $date) {
    $found = false;
    foreach ($result as $row) {
        if ($row['date'] == $date) {
            $weekdayIndex = date('w', strtotime($row['date']));
            $row['name'] = $weekdays[$weekdayIndex];
            $resultArray[] = $row;
            $dateArray[] = $row['date'];
            $totalAmountArray[] = $row['total_amount'];
            $found = true;
            break;
        }
    }
    if (!$found) {
        $weekdayIndex = date('w', strtotime($date));
        $resultArray[] = ['date' => $date, 'total_amount' => 0, 'name' => $weekdays[$weekdayIndex]];
        $dateArray[] = $date;
        $totalAmountArray[] = 0;
    }
}
$data['week_info']['date'] = $dateArray;
$data['week_info']['total_amount'] = $totalAmountArray;
$data['week_info1'] = $resultArray;
echo json_encode($data);

二、查询今年中每个月的数据

结果

代码

php 复制代码
//查询今年中每月的总金额数据
// 获取当前年份
$year = date('Y');
$result1 = Db::table('so_headers_all')
    ->field("DATE_FORMAT(FROM_UNIXTIME(creation_date), '%Y-%m') AS month, IFNULL(SUM(order_all_amount), 0) AS total_amount")
    ->whereTime('creation_date', '>=', strtotime($year . '-01-01'))
    ->whereTime('creation_date', '<=', strtotime($year . '-12-31'))
    ->group('month')
    ->select();
//去掉逗号,转换为
foreach ($result1 as &$item) {
    $item['total_amount'] = round(($item['total_amount'] / 10000),2);
}
// 构造最终结果数组
$dateArray1 = [];
$totalAmountArray1 = [];
for ($i = 1; $i <= 12; $i++) {
    //str_pad 函数用于在字符串的左侧(或右侧)填充指定字符,达到指定长度。这里,使用 str_pad 函数在 $i 左侧填充字符 '0',直到 $i 的长度达到 2。
    $month = str_pad($i, 2, '0', STR_PAD_LEFT);
    $dateArray1[] = $year . '-' . $month;
    $totalAmountArray1[$year . '-' . $month] = 0;
}
// 将查询结果填充到对应的月份位置
foreach ($result1 as $item) {
    $totalAmountArray1[$item['month']] = $item['total_amount'];
}
// 最终结果数组
foreach ($dateArray1 as $date) {
    $finalResult[] = [
        'date' => $date,
        'total_amount' => $totalAmountArray1[$date]
    ];
    $data['month_info']['date'][] = $date;
    $data['month_info']['total_amount'][] = $totalAmountArray1[$date];
}

三、查询近五年每年的总数据

结果

代码

php 复制代码
//查询近五年总金额数据
// 获取当前年份
$currentYear = date('Y');
// 构造最终结果数组
$data['year_info']['date'] = [];
$data['year_info']['total_amount'] = [];

for ($i = $currentYear - 4; $i <= $currentYear; $i++) {
    $year = (string) $i;

    $result = Db::table('so_headers_all')
        ->field("IFNULL(SUM(order_all_amount), 0) AS total_amount")
        ->whereTime('creation_date', '>=', strtotime($year . '-01-01'))
        ->whereTime('creation_date', '<=', strtotime($year . '-12-31'))
        ->find();
    $totalAmount = round(($result['total_amount'] / 10000), 2);
    $data['year_info']['date'][] = $year;
    $data['year_info']['total_amount'][] = $totalAmount;
}
echo json_encode($data);
相关推荐
逆袭的小黄鸭几秒前
深入剖析 JavaScript 执行上下文:代码运行的幕后机制
前端·javascript·面试
小old弟几秒前
闭包:从入门到“顿悟”的奇幻之旅
前端
晴殇i2 分钟前
抛弃 JavaScript 立即执行函数,这个方案更简洁更优雅
前端·javascript
码是生活2 分钟前
鸿蒙开发排坑:解决 resourceManager.getRawFileContent() 获取文件内容为空问题
前端·harmonyos
婷婷婷婷6 分钟前
v-copyText 自定义指令 —— 复制文本内容
前端
waylon111137 分钟前
【HOC】高阶组件在Vue老项目中的实战应用 - 模块任意排序
前端·vue.js·面试
柚几哥哥7 分钟前
IntelliJ IDEA全栈Git指南:从零构建到高效协作开发
java·git·intellij-idea
阳阳羊8 分钟前
Mpx 动画
前端
编程社区管理员9 分钟前
「2025最新版React+Ant Design+Router+TailwindCss全栈攻略:从零到实战,打造高颜值企业级应用
前端·react.js·前端框架
DJA_CR9 分钟前
解决在 TSX 中使用 `RouterView` + `KeepAlive` 不生效问题
前端·vue.js