PHP按年获取周数和日期

时间:2015-07-17 11:44:36

标签: php time

我想知道如何获得本周开始和结束的所有周数和日期。我在代码上完全是空的,所以我无法向你展示任何东西。我需要得到类似这个网站的内容:http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php

2 个答案:

答案 0 :(得分:3)

我建议如下,只需将年份更改为您喜欢的任何一年。

<?php

$year = 2014;

$weeks = getIsoWeeksInYear($year);

for($x=1; $x<=$weeks; $x++){
    $dates = getStartAndEndDate($x, $year);
    echo $x . " - " . $dates['week_start'] . ' - ' . $dates['week_end'] . "<br>";
}

function getIsoWeeksInYear($year) {
    $date = new DateTime;
    $date->setISODate($year, 53);
    return ($date->format("W") === "53" ? 53 : 52);
}

function getStartAndEndDate($week, $year) {
  $dto = new DateTime();
  $ret['week_start'] = $dto->setISODate($year, $week)->format('Y-m-d');
  $ret['week_end'] = $dto->modify('+6 days')->format('Y-m-d');
  return $ret;
}
?>

答案 1 :(得分:0)

试试这个:

$week = 1;
$year = 2014;
while($week <= 52)
{
        $time = strtotime("26 December $year", time());
        $day = date('w', $time);
        $time += ((7*$week)+1-$day)*24*3600;
        $start = date('Y-n-j', $time);
        $time += 6*24*3600;
        $end = date('Y-n-j', $time);

echo "weekno : ".$week." start date : ".$start." End date : ".$end."<br/>";
$week++;
$year++;
}