function getIsoWeeksInYear($year) {
$date = new DateTime;
$date->setISODate($year, 53);
return ($date->format("W") === "53" ? 53 : 52);
}
The o
date format gives the ISO-8601 year number. We can use this, and the fact that "invalid" dates are automatically rolled around to make valid ones (2011-02-31
gives 2011-03-03
), to determine if a given year has 53 weeks. If does not, then it must have 52.
See also, date format characters and DateTime::setISODate()
manual pages.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…