簡體   English   中英

從日期 PHP 獲取周數(在一年中)

[英]Get week number (in the year) from a date PHP

我想約會並計算出它的周數。

到目前為止,我有以下內容。 當它應該是 42 時,它返回 24。

<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[2], $duedt[1],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>

數字顛倒是錯的還是巧合? 還是我快到了?

今天,使用 PHP 的DateTime對象更好:

<?php
$ddate = "2012-10-18";
$date = new DateTime($ddate);
$week = $date->format("W");
echo "Weeknummer: $week";

這是因為在mktime() ,它是這樣的:

mktime(hour, minute, second, month, day, year);

因此,您的訂單是錯誤的。

<?php
$ddate = "2012-10-18";
$duedt = explode("-", $ddate);
$date  = mktime(0, 0, 0, $duedt[1], $duedt[2], $duedt[0]);
$week  = (int)date('W', $date);
echo "Weeknummer: " . $week;
?>
$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

使用 PHP 的日期函數
http://php.net/manual/en/function.date.php

date("W", $yourdate)

這得到今天的日期然后告訴一周的周數

<?php
 $date=date("W");
 echo $date." Week Number";
 ?>

就像一個建議:

<?php echo date("W", strtotime("2012-10-18")); ?>

可能比所有這些都簡單一點。

你可以做的其他事情:

<?php echo date("Weeknumber: W", strtotime("2012-10-18 01:00:00")); ?>
<?php echo date("Weeknumber: W", strtotime($MY_DATE)); ?>

多年來我一直試圖解決這個問題,我以為我找到了一個更短的解決方案,但不得不再次回到長篇大論。 此函數返回正確的 ISO 周表示法:

/**
 * 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;
}

我發現我的簡短解決方案錯過了 2018-12-31,因為它返回了 1801 而不是 1901。所以我不得不輸入這個正確的長版本。

要獲取北美日期的周數,我這樣做:

function week_number($n)
{
    $w = date('w', $n);
    return 1 + date('z', $n + (6 - $w) * 24 * 3600) / 7;
}

$n = strtotime('2022-12-27');
printf("%s: %d\n", date('D Y-m-d', $n), week_number($n));

並得到:

Tue 2022-12-27: 53

當一年有 53 周(如 2020 年)時,上面給出的大多數示例都會產生問題。 所以每四年你都會經歷一周的差異。 此代碼不會:

$thisYear = "2020";
$thisDate = "2020-04-24"; //or any other custom date
$weeknr = date("W", strtotime($thisDate)); //when you want the weeknumber of a specific week, or just enter the weeknumber yourself

$tempDatum = new DateTime();
$tempDatum->setISODate($thisYear, $weeknr);
$tempDatum_start = $tempDatum->format('Y-m-d');
$tempDatum->setISODate($thisYear, $weeknr, 7);
$tempDatum_end = $tempDatum->format('Y-m-d');

echo $tempDatum_start //will output the date of monday
echo $tempDatum_end // will output the date of sunday

如何使用IntlGregorianCalendar類?

要求:在開始使用IntlGregorianCalendar之前,請確保服務器上安裝了libicupecl/intl 所以在 CLI 上運行:

php -m

如果您在[PHP Modules]列表中看到intl ,那么您可以使用IntlGregorianCalendar

DateTime 與 IntlGregorianCalendarIntlGregorianCalendar並不比DateTime 但是IntlGregorianCalendar是它會給你周數作為int

例子:

$dateTime = new DateTime('21-09-2020 09:00:00');
echo $dateTime->format("W"); // string '39'

$intlCalendar = IntlCalendar::fromDateTime ('21-09-2020 09:00:00');
echo $intlCalendar->get(IntlCalendar::FIELD_WEEK_OF_YEAR); // integer 39
<?php
$ddate = "2012-10-18";
$duedt = explode("-",$ddate);
$date = mktime(0, 0, 0, $duedt[1], $duedt[2],$duedt[0]);
$week = (int)date('W', $date);
echo "Weeknummer: ".$week;
?>

你對 mktime 的參數有誤 - 需要是月/日/年,而不是日/月/年

要獲得日期 2018-12-31 的正確周數,請使用以下代碼

$day_count = date('N',strtotime('2018-12-31'));
$week_count = date('W',strtotime('2018-12-31'));    


if($week_count=='01' && date('m',strtotime('2018-12-31'))==12){
    $yr_count = date('y',strtotime('2018-12-31')) + 1;
}else{
    $yr_count = date('y',strtotime('2018-12-31'));
}

要在jalai 日歷中獲取周數,您可以使用:

$weeknumber = date("W"); //number week in year
$dayweek = date("w"); //number day in week
if ($dayweek == "6")
{
    $weeknumberint = (int)$weeknumber;
    $date2int++; 
    $weeknumber = (string)$date2int;
}

echo $date2;

結果:

15

周六周數變化

當您需要一年和一周時,會變得更加困難。
試着找出哪一周是 01.01.2017。
(這是 2016 年的第 52 周,從周一 26.12.2016 到周日 01.01.2017)。

經過長時間的搜索,我發現

strftime('%G-%V',strtotime("2017-01-01"))

結果:2016-52


https://www.php.net/manual/de/function.strftime.php
ISO-8601:1988 給定年份的周數,從一年的第一周開始,至少有 4 個工作日,星期一是一周的開始。 (01 到 53)


mysql 中的等價物是 DATE_FORMAT(date, '%x-%v') https://www.w3schools.com/sql/func_mysql_date_format.asp
星期一是一周的第一天的周(01 到 53)。


無法使用 date() 或 DateTime 找到相應的解決方案。
至少不是沒有像“+1day,上周一”這樣的解決方案。

非常簡單 只需一行:

<?php $date=date("W"); echo "Week " . $date; ?>"

您還可以,例如,像我需要的圖表一樣,減去以獲得前一周,例如:

<?php $date=date("W"); echo "Week " . ($date - 1); ?>

規則是一年的第一周是包含一年的第一個星期四的那一周。

我個人使用 Zend_Date 進行這種計算,獲得今天的星期就是這么簡單。 如果您使用日期,它們還有許多其他有用的功能。

$now = Zend_Date::now();
$week = $now->get(Zend_Date::WEEK);
// 10

您的代碼將起作用,但您需要翻轉第 4 個和第 5 個參數。

我會這樣做

$date_string = "2012-10-18";
$date_int = strtotime($date_string);
$date_date = date($date_int);
$week_number = date('W', $date_date);
echo "Weeknumber: {$week_number}.";

此外,在一周不查看該代碼后,您的變量名稱會讓您感到困惑,您應該考慮閱讀http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/

function last_monday($date) 
{
    if (!is_numeric($date))
        $date = strtotime($date);
    if (date('w', $date) == 1)
        return $date;
    else
        return date('Y-m-d',strtotime('last monday',$date));
}
$date = '2021-01-04';  //Enter custom date
$year = date('Y',strtotime($date));
$date1 = new DateTime($date);
$ldate = last_monday($year."-01-01");
$date2 = new DateTime($ldate);
$diff = $date2->diff($date1)->format("%a");
$diff = $diff/7;
$week = intval($diff) + 1;
echo $week;
//Returns 2.

試試這個解決方案

date( 'W', strtotime( "2017-01-01 + 1 day" ) );

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM