当前位置:网站首页>Processing of inconsistent week values obtained by PHP and MySQL
Processing of inconsistent week values obtained by PHP and MySQL
2022-07-26 09:06:00 【Angry devil】
Application scenarios
When processing statistics about weekly data , There's a problem : adopt MySQL Of week Function statistics “ Weeks ”, Than PHP Of date(“W”,time()) Acquired “ Weeks ” Small 1. Such as “2020-06-18 13:27:01”,MySQL What comes out is 24, and PHP What comes out is 25, This leads to a week of data not matching .
This is the first time I have encountered this problem , So I used it specially win10 The calendar in the corner counts , Find out “2020-06-18 13:27:01” It is 2020 In the first 25 Zhou , The problem is MySQL Of week Function .
The problem
Take a close look at , Find out MySQL Of week The function has a 2 Parameters ——mode?!
WEEK Two parameters of function :
date Is to get the date of the week .
mode Is an optional parameter , The logic used to determine the calculation of the number of weeks . It allows you to specify whether this week starts on Monday or Sunday , The number of weeks returned should be in 0 To 52 Between or 0 To 53 Between .
After reading this note, I didn't understand it at first , But I looked at the following table :
Pattern | The first day of the week | Range |
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 |
Suddenly I understood !
By default ,mode Parameter extraction 0, in other words , Weeks are counted from Sunday , Look again “2020-06-18 13:27:01”, Actually , The week it is in is “2020-06-14” To “2020-06-20”, instead of “2020-06-15” To “2020-06-21”
That's clear , It is caused by the difference of the first day of the week , that , Only unify it , Just OK 了 .
Solution
stay MySQL Of week Add the second parameter to the function “1”, namely WEEK(created_at,1), Look again ,MySQL and PHP The weeks taken are the same !
As for why there is 0-7 These eight models , Friends who understand , Welcome to leave a message ! thank you ~
边栏推荐
- JDBC database connection pool (Druid Technology)
- PHP和MySQL获取week值不一致的处理
- The essence of attack and defense strategy behind the noun of network security
- pycharm 打开多个项目的两种小技巧
- Laravel框架日志文件存放在哪里?怎么用?
- Day06 homework -- skill question 2
- Regular expression: judge whether it conforms to USD format
- js闭包:函数和其词法环境的绑定
- ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知
- [eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
猜你喜欢
随机推荐
李沐d2l(五)---多层感知机
JS - DataTables 关于每页显示数的控制
Database operation skills 7
Datax的学习笔记
How to quickly learn a programming language
mysql函数
Set of pl/sql
JVM触发minor gc的条件
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
JS file import of node
Pat grade a A1034 head of a gang
Database operation skills 6
李沐d2l(四)---Softmax回归
PHP page value transfer
[use of final keyword]
十大蓝筹NFT近半年数据横向对比
Media at home and abroad publicize that we should strictly grasp the content
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
Set of pl/sql -2
巴比特 | 元宇宙每日必读:元宇宙的未来是属于大型科技公司,还是属于分散的Web3世界?...