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 <= (8-$jan1weekday) && $jan1weekday > 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”.

$firstday = date(____1010____11__12

strtotime($EnglishDateTime, $time_now)
6
strtotime($EnglishDateTime, $time_now)
7
strtotime($EnglishDateTime, $time_now)
1$firstday
date( format, timestamp )
0

date( format, timestamp )
1

Output:

First day of this week: Monday - 04/02/2019

Bây giờ, thường là Chủ nhật được coi là ngày đầu tiên của một tuần. Vì vậy, trong PHP để có được Chủ nhật của ngày đầu tiên của một tuần, hãy xem xét Chủ nhật của tuần trước. Đó là để có được ngày đầu tiên (Chủ nhật) của một tuần cần phải có được Chủ nhật của tuần trước và để có được ngày đầu tiên (Chủ nhật) của tuần tới cần có được Chủ nhật của tuần này, v.v. Trong chuỗi thời gian. Vì vậy, để có được tuần trước, nó có thể sử dụng chuỗi thời gian như là -1 tuần và để có được ngày, nó cũng có thể phải bao gồm tên của chuỗi trong chuỗi thời gian.
PHP supports -ve indexing in time-string. So, in order to get the previous week it can use the time string as “-1 week” and to get the day it can also have to include the name of the day in time string.

Chương trình 2: Nhận ngày đầu tiên (Chủ nhật) trong tuần. Get the first day (Sunday) of the week.

$firstday = date(____1010____11__12

strtotime($EnglishDateTime, $time_now)
6
strtotime($EnglishDateTime, $time_now)
7
strtotime($EnglishDateTime, $time_now)
1$firstday
strtotime($EnglishDateTime, $time_now)
1
First day of this week: Monday - 04/02/2019
8
date( format, timestamp )
0

$firstday = date(____1010____11__12

strtotime($EnglishDateTime, $time_now)
6 1
strtotime($EnglishDateTime, $time_now)
1$firstday
strtotime($EnglishDateTime, $time_now)
1
First day of this week: Monday - 04/02/2019
8
date( format, timestamp )
0

$firstday = date(____1010____11__12

strtotime($EnglishDateTime, $time_now)
6 $firstday8
strtotime($EnglishDateTime, $time_now)
1$firstday
strtotime($EnglishDateTime, $time_now)
1
First day of this week: Monday - 04/02/2019
8
date( format, timestamp )
0

$firstday = date(

strtotime($EnglishDateTime, $time_now)
0
strtotime($EnglishDateTime, $time_now)
1____12(date22

strtotime($EnglishDateTime, $time_now)
6 date5
strtotime($EnglishDateTime, $time_now)
1$firstday
date( format, timestamp )
0

date( format, timestamp )
1

Output:

First day of this week: Sunday - 03/02/2019
First day of last week: Sunday - 27/01/2019
First day of next week: Sunday - 10/02/2019
First day of week after next week : Sunday - 17/02/2019


Đây là số tuần nào?

Tuần 40. Tuần 40 là từ thứ Hai, ngày 3 tháng 10 năm 2022 cho đến (và bao gồm) Chủ nhật, ngày 9 tháng 10 năm 2022.. Week 40 is from Monday, October 3, 2022 until (and including) Sunday, October 9, 2022.

Việc sử dụng hàm date () trong PHP là gì?

Hàm ngày () định dạng một ngày và giờ cục bộ và trả về chuỗi ngày được định dạng.formats a local date and time, and returns the formatted date string.

Làm thế nào để bạn kiểm tra xem một ngày trong vòng một tuần ở PHP?

Bước 1: Tìm bắt đầu và ngày cuối cùng của tuần $ Firstday = date ("y-m-d", strtotime ('Chủ nhật tuần trước'));$ Cuối cùng = ngày ("y-m-d", strtotime ('Chủ nhật tuần này')) ;.
Bước 2 :.

Làm thế nào để có được tuần hiện tại trong năm trong PHP?

PHP $ ddate = "2012-10-18";$ dedt = explode ("-", $ ddate);$ date = mktime (0, 0, 0, $ duedt [2], $ duedt [1], $ duedt [0]);$ tuần = (int) ngày ('w', $ ngày);Echo "Weeknummer:".$week = (int)date('W', $date); echo "Weeknummer: ".