dp0518 2016-12-09 14:25
浏览 11

PHP - 获取下个月的mysql数据

I have a database that contains payment amounts and dates for the payments.

example:

date = '2016-12-15'
transaction_amount = '250.00'

I then have a dashboard that calculates the sum of the payments for the current month, previous month and following month.

For the current years calculations my select works fine. It looks for any dates with that month and year. The issue is when the year changes, the results are not for the intended month. For example, for Dec 2016, I want Jan 2017 but instead I am getting Jan 2016.

How do I continuously calculate the current month, previous month and next month data and take into consideration the change from year to year.

Here is my code:

$getlastmonth = mysqli_query("SELECT sum(transaction_amount) as TOTAL 
                            from customer_payment 
                            WHERE YEAR(date)=YEAR(CURDATE()) 
                            and MONTH(date)=MONTH(CURDATE()- INTERVAL 1 MONTH)
                            and status='completed'");

$getthismonth = mysqli_query("SELECT sum(transaction_amount) as TOTAL 
                            from customer_payment 
                            WHERE YEAR(date)=YEAR(CURDATE()) 
                            and MONTH(date)=MONTH(CURDATE()) 
                            and status='completed'");

$getnextmonth = mysqli_query("SELECT sum(transaction_amount) as TOTAL 
                            from customer_payment 
                            WHERE YEAR(date)=YEAR(CURDATE()) 
                            and MONTH(date)=MONTH(CURDATE()+ INTERVAL 1 MONTH) 
                            and status='completed'");

I've tried using answers found on S.O. but they all seem to just get the exact day of the next month instead of the date range (start to end of the month).

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 keil里为什么main.c定义的函数在it.c调用不了
    • ¥50 切换TabTip键盘的输入法
    • ¥15 可否在不同线程中调用封装数据库操作的类
    • ¥15 微带串馈天线阵列每个阵元宽度计算
    • ¥15 keil的map文件中Image component sizes各项意思
    • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
    • ¥15 划分vlan后,链路不通了?
    • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 Centos / PETGEM