当前位置:网站首页>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 ~
边栏推荐
- Introduction to excellent verilog/fpga open source project (30) - brute force MD5
- 谷粒学院的全部学习源码
- [recommended collection] MySQL 30000 word essence summary - query and transaction (III)
- Babbitt | metauniverse daily must read: does the future of metauniverse belong to large technology companies or to the decentralized Web3 world
- 基于序的评价指标 (特别针对推荐系统和多标签学习)
- tornado之多进程服务
- CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
- Set of pl/sql
- 187. Repeated DNA sequence
- Horizontal comparison of the data of the top ten blue chip NFTs in the past half year
猜你喜欢

Web overview and b/s architecture

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

NPM add source and switch source

围棋智能机器人阿法狗,阿尔法狗机器人围棋

Day06 operation -- addition, deletion, modification and query

NFT与数字藏品到底有何区别?

Innovus卡住,提示X Error:

CF1481C Fence Painting

The essence of attack and defense strategy behind the noun of network security

Canal 的学习笔记
随机推荐
zsh: command not found: nvm
CSDN Top1 "how does a Virgo procedural ape" become a blogger with millions of fans through writing?
Canal 的学习笔记
PAT 甲级 A1034 Head of a Gang
Day 6 summary & database operation
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
Clean the label folder
分布式跟踪系统选型与实践
Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation
数据库操作技能7
838. 堆排序
本地缓存
Codeworks DP collection
Pop up window in Win 11 opens with a new tab ---firefox
对象型的集合按某个属性的值进行去重
MySQL strengthen knowledge points
公告 | FISCO BCOS v3.0-rc4发布,新增Max版,可支撑海量交易上链
Announcement | FISCO bcos v3.0-rc4 is released, and the new Max version can support massive transactions on the chain
JS file import of node
李沐d2l(六)---模型选择