PHP日期('W')vs MySQL YEARWEEK(now()) [英] PHP date('W') vs MySQL YEARWEEK(now())

查看:431
本文介绍了PHP日期('W')vs MySQL YEARWEEK(now())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下为什么这两个给出不同的结果?

Can someone kindly explain me why these two give different results?

我用PHP执行。

date("YW",mktime(0, 0, 0, 3, 22 , 2013)); // outputs 201312

当我执行这个MySQL

And when I execute this with MySQL

SELECT YEARWEEK(now()); // outputs 201311


推荐答案

您需要指定模式3 mysql YEARWEEK调用:

You need to specify mode 3 on the mysql YEARWEEK call:

SELECT YEARWEEK(now(),3); 

PHP date() placeholder W 根据 ISO 8601规范返回周号这意味着星期一(不是星期日)的星期开始,一年的第一个星期是第1个(不是0个),那一周是第一个在新的一年中至少有4天的时间(所以这是包含新的一周今年第一个星期四)。根据 MySQL WEEK的文档功能,选项的组合是模式3.

The PHP date() placeholder W returns the week number according to the ISO 8601 specification. That means weeks start on Monday (not Sunday), the first week of the year is number 1 (not 0), and that week is the first one that has at least 4 days in the new year (so it's the week containing the new year's first Thursday). According to the documentation for the MySQL WEEK function, that combination of options is mode 3.

另外,将Alles的注释拉入可接受的答案,因为重要的是:占位符 Y W 不要一起去。如果您想要使用ISO周号的年份,则应使用 o 而不是 Y 。例如,考虑2014年12月29日星期一:

Also, to pull Alles's note into the accepted answer because it's important: the placeholders Y and W don't go together. If you want the year that goes with the ISO week number, you should use o instead of Y. For example, consider Monday, December 29th, 2014:

date('YW', mktime(0,0,0,12,29,2014));  #=> 201401 : 1st week of 2014??
date('oW', mktime(0,0,0,12,29,2014));  #=> 201501 : better

这篇关于PHP日期('W')vs MySQL YEARWEEK(now())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆