简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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