Hướng dẫn how to find week from date in php? - làm thế nào để tìm tuần kể từ ngày trong php?

Tôi đã cố gắng giải quyết câu hỏi này trong nhiều năm nay, tôi nghĩ rằng tôi đã tìm thấy một giải pháp ngắn hơn nhưng phải quay lại một lần nữa với câu chuyện dài. Hàm này mang lại ký hiệu tuần ISO quyền:

/**
 * calcweek["2018-12-31"] => 1901
 * This function calculates the production weeknumber according to the start on 
 * monday and with at least 4 days in the new year. Given that the $date has
 * the following format Y-m-d then the outcome is and integer.
 *
 * @author M.S.B. Bachus
 *
 * @param date-notation PHP "Y-m-d" showing the data as yyyy-mm-dd
 * @return integer
 **/
function calcweek[$date] {
  // 1. Convert input to $year, $month, $day
  $dateset      = strtotime[$date];
  $year         = date["Y", $dateset];
  $month        = date["m", $dateset];
  $day          = date["d", $dateset];

  $referenceday = getdate[mktime[0,0,0, $month, $day, $year]];
  $jan1day      = getdate[mktime[0,0,0,1,1,$referenceday[year]]];

  // 2. check if $year is a  leapyear
  if [ [$year%4==0 && $year%100!=0] || $year%400==0] {
    $leapyear = true;
  } else {
    $leapyear = false;
  }

  // 3. check if $year-1 is a  leapyear
  if [ [[$year-1]%4==0 && [$year-1]%100!=0] || [$year-1]%400==0 ] {
    $leapyearprev = true;
  } else {
    $leapyearprev = false;
  }

  // 4. find the dayofyearnumber for y m d
  $mnth = array[0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
  $dayofyearnumber = $day + $mnth[$month-1];
  if [ $leapyear && $month > 2 ] { $dayofyearnumber++; }

  // 5. find the jan1weekday for y [monday=1, sunday=7]
  $yy = [$year-1]%100;
  $c  = [$year-1] - $yy;
  $g  = $yy + intval[$yy/4];
  $jan1weekday = 1+[[[[intval[$c/100]%4]*5]+$g]%7];

  // 6. find the weekday for y m d
  $h = $dayofyearnumber + [$jan1weekday-1];
  $weekday = 1+[[$h-1]%7];

  // 7. find if y m d falls in yearnumber y-1, weeknumber 52 or 53
  $foundweeknum = false;
  if [ $dayofyearnumber  4 ] {
    $yearnumber = $year - 1;
    if [ $jan1weekday = 5 || [ $jan1weekday = 6 && $leapyearprev ]] {
      $weeknumber = 53;
    } else {
      $weeknumber = 52;
    }
    $foundweeknum = true;
  } else {
    $yearnumber = $year;
  }

  // 8. find if y m d falls in yearnumber y+1, weeknumber 1
  if [ $yearnumber == $year && !$foundweeknum] {
    if [ $leapyear ] {
      $i = 366;
    } else {
      $i = 365;
    }
    if [ [$i - $dayofyearnumber] < [4 - $weekday] ] {
      $yearnumber = $year + 1;
      $weeknumber = 1;
      $foundweeknum = true;
    }
  }

  // 9. find if y m d falls in yearnumber y, weeknumber 1 through 53
  if [ $yearnumber == $year && !$foundweeknum ] {
    $j = $dayofyearnumber + [7 - $weekday] + [$jan1weekday - 1];
    $weeknumber = intval[ $j/7 ];
    if [ $jan1weekday > 4 ] { $weeknumber--; }
  }

  // 10. output iso week number [YYWW]
  return [$yearnumber-2000]*100+$weeknumber;
}

Tôi phát hiện ra rằng giải pháp ngắn của tôi đã bỏ lỡ 2018-12-31 vì nó đã trả lại năm 1801 thay vì năm 1901. Vì vậy, tôi phải đưa vào phiên bản dài này là chính xác.

Sử dụng hàm strtotime [] để có được ngày đầu tiên trong tuần bằng PHP. Hàm này trả về thời gian thời gian biến thời gian mặc định và sau đó sử dụng hàm ngày [] để chuyển đổi ngày Dấu thời gian thành ngày có thể hiểu được.

hàm strtotime []: hàm strtotime [] trả về kết quả trong dấu thời gian bằng cách phân tích chuỗi thời gian.syntax: The strtotime[] function returns the result in timestamp by parsing the time string.
Syntax:

strtotime[$EnglishDateTime, $time_now]

Tham số: hàm strtotime [] chấp nhận hai tham số như đã đề cập ở trên và được mô tả dưới đây: The strtotime[] function accepts two parameters as mentioned above and described below:

  • $ EnglishDateTime: Nó chỉ định mô tả thời gian ngày văn bản tiếng Anh, đại diện cho ngày hoặc thời gian được trả về. Hàm phân tích chuỗi và trả về thời gian tính bằng giây. Nó là tham số cần thiết. It specifies the English textual date-time description, which represents the date or time to be returned. The function parses the string and returns the time in seconds. It is required parameter.
  • $ time_now: Tham số này chỉ định dấu thời gian được sử dụng để tính giá trị được trả về. Nó là một tham số tùy chọn. This parameter specifies the timestamp used to calculate the returned value. It is an optional parameter.

date [] hàm: hàm ngày [] trả về định dạng ngày dễ hiểu hơn và có thể đọc được của con người. The date[] function returns more understandable and human readable format of date.
Syntax:

date[ format, timestamp ]

Tham số: Hàm này chấp nhận hai tham số như đã đề cập ở trên và được mô tả dưới đây: This function accepts two parameters as mentioned above and described below:

  • Định dạng: Nó chỉ định định dạng cho ngày và thời gian mà kết quả sẽ được hiển thị. It specify the format for date and time in which the result will be display.
  • Dấu thời gian: Đây là biến thời gian mặc định mà ngày sẽ được tạo ra. It is the default time variable from which the date will be generated.

Lưu ý: Trong PHP, tuần bắt đầu với thứ Hai, vì vậy nếu chuỗi thời gian được đưa ra như là trong tuần này, đầu ra sẽ là dấu thời gian của thứ Hai có thể đọc được bằng cách chuyển nó qua hàm ngày []. In PHP, the week starts with Monday, so if the time-string is given as “this week” the output will be timestamp of Monday which can make readable by passing it through date[] function.

Chương trình 1: Nhận mặc định ngày đầu tiên trong tuần khi chuỗi thời gian là trong tuần này. Get the default first day of a week when the time-string is “this week”.

Chủ Đề