PHP+MySQL counts the data of the current month, current week, and current day, and sorts the corresponding fields in groups

In our development process, we often come across ranking members or merchants according to the current month, current week or current day; of course, you can first obtain the used data, and then according to The function processes the data accordingly. Next, what I want to show you is to directly operate MySQL, and use some statistical functions of MySQL to conveniently filter data.

First of all, I will introduce you to use php to get timestamps of the current day, current week, current month, and yesterday;
  • Get today's start timestamp and end timestamp

	$today_start=mktime(0,0,0,date('m'),date('d'),date('Y'));
	$today_end=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
  • Get yesterday's start timestamp and end timestamp

	$yesterday_start=mktime(0,0,0,date('m'),date('d')-1,date('Y'));
	$yesterday_end=mktime(0,0,0,date('m'),date('d'),date('Y'))-1;
  • Get the start and end timestamps of last week

	$lastweek_start=mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));
	$lastweek_end=mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));
  • Get the start and end timestamps of this week

	$thisweek_start=mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y'));
	$thisweek_end=mktime(23,59,59,date('m'),date('d')-date('w')+7,date('Y'));
  • Get the start and end timestamps of this month

	$thismonth_start=mktime(0,0,0,date('m'),1,date('Y'));
	$thismonth_end=mktime(23,59,59,date('m'),date('t'),date('Y'));
 The following are some basic operations for MySQL to obtain the data of the day, week, and month;
  • Query the data of the day

	SELECT * FROM table name WHERE TO_DAYS(time field)=TO_DAYS(NOW());
  • Query yesterday's data

	SELECT * FROM table name WHERE TO_DAYS(NOW())-TO_DAYS(time field)=1;
  • Query the data of the current week

	SELECT * FROM table name WHERE YEARWEEK(DATE_FORMAT(time field,'%Y-%m-%d'))=YEARWEEK(NOW());
  • Query last week's data

	SELECT * FROM table name WHERE YEARWEEK(DATE_FORMAT(time field,'%Y-%m-%d'))=YEARWEEK(NOW())-1;
  • Query the data of the current month

	SELECT * FROM table name WHERE DATE_FORMAT(time field,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m');
  • Query the data of the previous month

	SELECT * FROM table name WHERE PERIOD_DIFF(DATE_FORMAT(NOW(),'%Y%m'),DATE_FORMAT(time field,'%Y%m'))=1;
  • Query the data of the year

	SELECT * FROM table name WHERE YEAR(time field) =YEAR(NOW());
  • Query the data of the last 7 days

	SELECT * FROM table name WHERE DATE_SUB(CURDATE(),INTERVAL 7 DAY)<=DATE(time field);

Next, we will use some of the above query operations to process data for the income ranking function shown in the figure below;
Write picture description here

  • The data format is as shown in the figure above, and the code is directly listed below:

switch ($style) {

			case 'total'://总榜
				// $total = M('income_log')->group('userid')->sum($field);
				$total = M('')->query("SELECT userid,SUM($field) as sum FROM income_log GROUP BY userid ORDER BY SUM($field) DESC LIMIT 10");
				break;
			case 'day'://日榜
				$total = M('')->query("SELECT userid,SUM($field) as sum FROM income_log where TO_DAYS(date)=TO_DAYS(NOW()) GROUP BY userid ORDER BY SUM($field) DESC LIMIT 10"); 
				break;
			case 'week'://周榜
				$total = M('')->query("SELECT userid,SUM($field) as sum FROM income_log where YEARWEEK(DATE_FORMAT(date,'%Y-%m-%d'))=YEARWEEK(NOW()) GROUP BY userid ORDER BY SUM($field) DESC LIMIT 10"); 
				break;
			case 'month'://月榜
				$total = M('')->query("SELECT userid,SUM($field) as sum FROM income_log where DATE_FORMAT(date,'%Y%m')=DATE_FORMAT(CURDATE(),'%Y%m') GROUP BY userid ORDER BY SUM($field) DESC LIMIT 10"); 
				break;
			default: 
				break;
		}

The following is the result of the obtained data, which only needs to be processed; here, I group the data by user id through group by to facilitate the acquisition and ranking of user information;

Array
(
    [0] => Array
        (
            [userid] => 2
            [sum] => 6.00
        )

    [1] => Array
        (
            [userid] => 1
            [sum] => 6.00
        )

    [2] => Array
        (
            [userid] => 3
            [sum] => 1.00
        )

    [3] => Array
        (
            [userid] => 4
            [sum] => 0.20
        )

)

Reprint: PHP+MySQL counts the data of the current month, current week, and current day, and sorts the corresponding fields in groups 

Week in MySQL (WEEK, YEARWEEK)

Week (WEEK, YEARWEEK) in MySQL - Dandelion Cloud

In mysql, a week is from Sunday to Monday by default, which is the same as the habit abroad, while in China, a week is counted from Monday to Sunday. Therefore, special attention is required when counting such as "this week's data".

I saw a lot of articles on the Internet and wrote a lot of codes to deal with this point. In fact, this point has been taken into account in the WEEK and YEARWEEK functions provided by mysql.

Introduction to WEEK function

Usually, a normal year has 365 days and a leap year has 366 days. A year can be divided into many weeks, each week has 7 days. So in a year, we often have 365/7 = 52 weeks, with weeks ranging from 1 to 52.

To see which week number a given date belongs to, you can use the WEEK function as follows:

WEEK(date, mode);

The WEEK function accepts two parameters:

  • date is the date to get the week number from.
  • mode is an optional parameter that determines the logic for week number calculation. It allows you to specify whether the week starts on Monday or Sunday, and the returned week number should be between 0 and 52 or 0 and 53.

If the mode parameter is omitted, by default the WEEK function will use the value of the default_week_format system variable.

To get the current value of the default_week_format variable, use the SHOW VARIABLES statement as follows:

  1. mysql> SHOW VARIABLES LIKE ‘default_week_format’;
  2. +——————————-+———-+
  3. | Variable_name | Value |
  4. +——————————-+———-+
  5. | default_week_format | 0 |
  6. +——————————-+———-+
  7. 1 row in set

In our server, the default value of default_week_format is 0. The following table shows how the mode parameter affects the WEEK function:

model

first day of the week

scope

0

Sunday

0-53

1

Monday

0-53

2

Sunday

1-53

3

Monday

1-53

4

Sunday

0-53

5

Monday

0-53

6

Sunday

1-53

7

Monday

1-53

"There are more than 4 days in this year" in the above table means:

  • If the week contains January 1 and has 4 or more days in the new year, then the week is week 1 of the year.
  • Otherwise, the number for this week is the last week of the previous year, and the next week is the first week of the year.

So when the year needs to be considered, it is more appropriate to use the YEARWEEK function.

example

According to the Chinese custom, we count Monday as the first day of the week, and use the WEEK function and the YEARWEEK function to obtain the week of 2019-07-11

select WEEK(‘2019-07-11’,1);

The return value is 28

select YEARWEEK(‘2019-07-11’,1);

The return value is 201928

 

Guess you like

Origin blog.csdn.net/qq_32307773/article/details/125982042
Recommended