I was trying to transform a date \"YYYY/MM/DD\" into \"YYYY/WW\" format, so I can store the weekly aggregation data, which has a structure below
aggre_date(YYYY/
You need to use o
for the ISO year:
ISO-8601 year number. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. (added in PHP 5.1.0)
$dateTime = new DateTime("2014-12-30");
echo $dateTime->format("o-W")."\n";
$dateTime = new DateTime("2014-01-01");
echo $dateTime->format("o-W")."\n";
2015-01
2014-01
Demo