<?php
/*
その年の第何週かを求める
*/
function get_nth_week($d) {
// その年の 1月1日
$d0 = new DateTime($d->format('Y') . '-1-1');
// 1月1日の週の日曜日にする
$d0->sub(new DateInterval('P' . $d0->format('w') . 'D'));
// 1月1日の週の日曜日から、何週間離れているか
$diff = ($d->getTimestamp() - $d0->getTimestamp()) / 60 / 60 / 24 / 7;
// 整数化
// ゼロ始まりなので、+1 して返す
return $diff + 1;
}
// 確認(その1)
$d = new DateTime('now');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
// 確認(その2):バレンタインデー
$d = new DateTime('2014-2-14');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
// 確認(その3):去年
$d = new DateTime('2013-2-14');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
$d = new DateTime('2014-12-30');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
$d = new DateTime('2013-12-30');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
$d = new DateTime('2012-12-30');
$w = get_nth_week($d);
echo $d->format(DateTime::ATOM) . ' ' . $w . ' ' . $d->format('W') . "\n";
?>