从日期PHP中获得周号(年)。

时间:2022-10-16 08:44:36

I want to take a date and work out its week number.

我想约个时间,算出它的星期号。

So far, I have the following. It is returning 24 when it should be 42.

到目前为止,我有如下内容。它应该是42。

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

Is it wrong and a coincidence that the digits are reversed? Or am I nearly there?

数字是颠倒的,这是错误的吗?或者我快到了?

11 个解决方案

#1


112  

Today, using PHP's DateTime objects is better:

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

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

It's because in mktime(), it goes like this:

因为在mktime()中,它是这样的:

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

Hence, your order is wrong.

因此,您的订单是错误的。

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

#2


46  

$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

#3


9  

Use PHP's date function
http://php.net/manual/en/function.date.php

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

date("W", $yourdate)

#4


7  

Just as a suggestion:

就像一个建议:

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

Might be a little simpler than all that lot.

可能比所有这些都简单。

Other things you could do:

其他你可以做的事情:

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

#5


5  

This get today date then tell the week number for the week

这就得到了今天的日期,然后告诉周号。

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

#6


0  

Your code will work but you need to flip the 4th and the 5th argument.

你的代码可以工作,但是你需要翻转第4和第5个参数。

I would do it this way

我会这样做。

$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}.";

Also, your variable names will be confusing to you after a week of not looking at that code, you should consider reading http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/

另外,如果您的变量名在一周没有查看代码之后会让您感到困惑,那么您应该考虑阅读http://net.tutsplus.com/tutorials/php/why-youre-a-bad php-programmer/。

#7


0  

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

You had the params to mktime wrong - needs to be Month/Day/Year, not Day/Month/Year

你有对mktime错误的params -需要是月/日/年,而不是日/月/年。

#8


0  

The rule is that the first week of a year is the week that contains the first Thursday of the year.

规则是,一年的第一个星期是包含一年中的第一个星期四的一周。

I personally use Zend_Date for this kind of calculation and to get the week for today is this simple. They have a lot of other useful functions if you work with dates.

我个人使用Zend_Date进行这种计算,得到今天的这个星期就是这么简单。如果你和日期一起工作,他们还有很多其他有用的功能。

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

#9


0  

try this solution

试试这个解决方案

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

#10


0  

I have tried to solve this question for years now, I thought I found a shorter solution but had to come back again to the long story. This function gives back the right ISO week notation:

多年来,我一直试图解决这个问题,我想我找到了一个更短的解决方案,但不得不重新回到这个漫长的故事。这个函数给出了正确的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;
}

I found out that my short solution missed the 2018-12-31 as it gave back 1801 instead of 1901. So I had to put in this long version which is correct.

我发现我的短期解决方案错过了2018-12-31,因为它是在1801年,而不是1901年。所以我必须把这个长版本写下来,这是正确的。

#11


-1  

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.

#1


112  

Today, using PHP's DateTime objects is better:

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

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

It's because in mktime(), it goes like this:

因为在mktime()中,它是这样的:

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

Hence, your order is wrong.

因此,您的订单是错误的。

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

#2


46  

$date_string = "2012-10-18";
echo "Weeknummer: " . date("W", strtotime($date_string));

#3


9  

Use PHP's date function
http://php.net/manual/en/function.date.php

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

date("W", $yourdate)

#4


7  

Just as a suggestion:

就像一个建议:

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

Might be a little simpler than all that lot.

可能比所有这些都简单。

Other things you could do:

其他你可以做的事情:

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

#5


5  

This get today date then tell the week number for the week

这就得到了今天的日期,然后告诉周号。

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

#6


0  

Your code will work but you need to flip the 4th and the 5th argument.

你的代码可以工作,但是你需要翻转第4和第5个参数。

I would do it this way

我会这样做。

$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}.";

Also, your variable names will be confusing to you after a week of not looking at that code, you should consider reading http://net.tutsplus.com/tutorials/php/why-youre-a-bad-php-programmer/

另外,如果您的变量名在一周没有查看代码之后会让您感到困惑,那么您应该考虑阅读http://net.tutsplus.com/tutorials/php/why-youre-a-bad php-programmer/。

#7


0  

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

You had the params to mktime wrong - needs to be Month/Day/Year, not Day/Month/Year

你有对mktime错误的params -需要是月/日/年,而不是日/月/年。

#8


0  

The rule is that the first week of a year is the week that contains the first Thursday of the year.

规则是,一年的第一个星期是包含一年中的第一个星期四的一周。

I personally use Zend_Date for this kind of calculation and to get the week for today is this simple. They have a lot of other useful functions if you work with dates.

我个人使用Zend_Date进行这种计算,得到今天的这个星期就是这么简单。如果你和日期一起工作,他们还有很多其他有用的功能。

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

#9


0  

try this solution

试试这个解决方案

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

#10


0  

I have tried to solve this question for years now, I thought I found a shorter solution but had to come back again to the long story. This function gives back the right ISO week notation:

多年来,我一直试图解决这个问题,我想我找到了一个更短的解决方案,但不得不重新回到这个漫长的故事。这个函数给出了正确的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;
}

I found out that my short solution missed the 2018-12-31 as it gave back 1801 instead of 1901. So I had to put in this long version which is correct.

我发现我的短期解决方案错过了2018-12-31,因为它是在1801年,而不是1901年。所以我必须把这个长版本写下来,这是正确的。

#11


-1  

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.