简体   繁体   English

获取具有年份和年份的周数的每周星期一的日期

[英]get the date of Monday of the week having the year and the week number of the year

How could I get the date of the day Monday of a week having the year and number of week in the year?我怎样才能获得一周中星期一的日期,其中包含一年中的年份和周数?

For example, if the year is 2020 and the number of the week in the year is 01, the expected result is 2019-12-30例如,如果年份是 2020 并且该年中的周数为 01,则预期结果为 2019-12-30

Hope this is what you are looking for :)希望这是你正在寻找的:)

<?php

$week_number = '06';
$year_number = '2020';

function getStartAndEndDate($week, $year) {
  $date = new DateTime();
  $date->setISODate($year, $week);
  for ($i=0; $i < 7; $i++) {
    $array[$i] = $date->format('Y-m-d');
    $date->modify('+1 days');
  }
  return $array;
}

$week_array = getStartAndEndDate($week_number,$year_number);

echo $week_array[0]; // 0 = monday, 1 = tuesday, and so on :)

?>
<?php
$week = 1;
$year = 2020;

$dateTime = date_create(sprintf("%04dW%02d",$year,$week)); //always a monday

echo $dateTime->format('Y-m-d');  //2019-12-30

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM