簡體   English   中英

獲取具有年份和年份的周數的每周星期一的日期

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

我怎樣才能獲得一周中星期一的日期,其中包含一年中的年份和周數?

例如,如果年份是 2020 並且該年中的周數為 01,則預期結果為 2019-12-30

希望這是你正在尋找的:)

<?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