当前位置:网站首页>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 ~
边栏推荐
- Canal 的学习笔记
- Error: Cannot find module ‘umi‘ 问题处理
- TCP solves the problem of short write
- Kotlin properties and fields
- 2022化工自动化控制仪表操作证考试题模拟考试平台操作
- Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
- 堆外内存的使用
- The lessons of 2000. Web3 = the third industrial revolution?
- at、crontab
- The largest number of statistical absolute values --- assembly language
猜你喜欢

The Child and Binary Tree-多项式开根求逆

Study notes of dataX

day06 作业--技能题6

Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output

Probability model in machine learning

ext4文件系统打开了DIR_NLINK特性后,link_count超过65000的后使用link_count=1来表示数量不可知

CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?

Pop up window in Win 11 opens with a new tab ---firefox

CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?

The lessons of 2000. Web3 = the third industrial revolution?
随机推荐
力扣——二叉树剪枝
Matlab 绘制阴影误差图
Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
Datax的学习笔记
Clean the label folder
JDBC数据库连接池(Druid技术)
Typescript encryption tool passwordencoder
Day06 homework - skill question 7
李沐d2l(六)---模型选择
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
SQL入门——组合表
js闭包:函数和其词法环境的绑定
NFT与数字藏品到底有何区别?
十大蓝筹NFT近半年数据横向对比
Web概述和B/S架构
Day06 homework -- skill question 2
数据库操作 技能6
Center an element horizontally and vertically
Espressif plays with the compilation environment
The largest number of statistical absolute values --- assembly language