为什么following code会返回2017-52
而不是预期2017-01
?
date('Y-W', strtotime('2017-01-01')); // returns "2017-52"
编辑: 正如here所述,有适用于一年中第一周的规则。不幸的是,此行为可能导致日期FROM变得大于日期TO。为了解决这个问题,我使用以下代码来计算周:
function getWeek($timestamp)
{
$ts1 = mktime(0,0,0,1,1,date('Y', $timestamp));
$ts2 = mktime(0,0,0,date('n', $timestamp),date('j', $timestamp),date('Y', $timestamp));
$days = ceil(($ts2 - $ts1) / 86400);
$week = ceil($days / 7);
$yearWeek = date('Y', $timestamp).'-'.str_pad($week, 2, '0', STR_PAD_LEFT); // gives 2017-00 for January 1st
return $yearWeek;
}