从 PHP 中的日期获取月中的周数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32615861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get week number in month from date in PHP?
提问by Asif Hussain
I have an array of random dates (not coming from MySQL). I need to group them by the week as Week1, Week2, and so on upto Week5.
我有一组随机日期(不是来自 MySQL)。我需要按周将它们分组为 Week1、Week2,依此类推直到 Week5。
What I have is this:
我有的是这个:
$dates = array('2015-09-01','2015-09-05','2015-09-06','2015-09-15','2015-09-17');
What I need is a function to get the week number of the month by providing the date.
我需要的是通过提供日期来获取当月周数的函数。
I know that I can get the weeknumber by doing
date('W',strtotime('2015-09-01'));
but this week number is the number between year (1-52) but I need the week number of the month only, e.g. in Sep 2015 there are 5 weeks:
我知道我可以通过这样做来获得
date('W',strtotime('2015-09-01'));
周数,但是这个周数是年份(1-52)之间的数字,但我只需要一个月的周数,例如在 2015 年 9 月有 5 周:
- Week1 = 1st to 5th
- Week2 = 6th to 12th
- Week3 = 13th to 19th
- Week4 = 20th to 26th
- Week5 = 27th to 30th
- 第 1 周 = 第 1 至第 5
- 第 2 周 = 第 6 至第 12
- 第 3 周 = 第 13 至 19 日
- 第 4 周 = 20 日至 26 日
- 第 5 周 = 第 27 至 30
I should be able to get the week Week1 by just providing the date e.g.
我应该能够通过提供日期来获得第 Week1 周,例如
$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;
回答by Anders
I think this relationship should be true and come in handy:
我认为这种关系应该是真实的并且派上用场:
Week of the month = Week of the year - Week of the year of first day of month + 1
Implemented in PHP you get this:
在 PHP 中实现你得到这个:
function weekOfMonth($date) {
//Get the first day of the month.
$firstOfMonth = strtotime(date("Y-m-01", $date));
//Apply above formula.
return intval(date("W", $date)) - intval(date("W", $firstOfMonth)) + 1;
}
To get weeks that starts with sunday, simply replace both date("W", ...)
with strftime("%U", ...)
.
要获得以星期日开始的星期,只需将两者都替换date("W", ...)
为strftime("%U", ...)
.
回答by Matteo Tassinari
You can use the function below, fully commented:
您可以使用下面的功能,完全评论:
/**
* Returns the number of week in a month for the specified date.
*
* @param string $date
* @return int
*/
function weekOfMonth($date) {
// estract date parts
list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date)));
// current week, min 1
$w = 1;
// for each day since the start of the month
for ($i = 1; $i <= $d; ++$i) {
// if that day was a sunday and is not the first day of month
if ($i > 1 && date('w', strtotime("$y-$m-$i")) == 0) {
// increment current week
++$w;
}
}
// now return
return $w;
}
回答by Goendg
The corect way is
正确的方法是
function weekOfMonth($date) {
$firstOfMonth = date("Y-m-01", strtotime($date));
return intval(date("W", strtotime($date))) - intval(date("W", strtotime($firstOfMonth)));
}
回答by Asif Hussain
I have created this function on my own, which seems to work correctly. In case somebody else have a better way of doing this, please share.. Here is what I have done.
我自己创建了这个函数,它似乎工作正常。如果其他人有更好的方法来做到这一点,请分享..这是我所做的。
function weekOfMonth($qDate) {
$dt = strtotime($qDate);
$day = date('j',$dt);
$month = date('m',$dt);
$year = date('Y',$dt);
$totalDays = date('t',$dt);
$weekCnt = 1;
$retWeek = 0;
for($i=1;$i<=$totalDays;$i++) {
$curDay = date("N", mktime(0,0,0,$month,$i,$year));
if($curDay==7) {
if($i==$day) {
$retWeek = $weekCnt+1;
}
$weekCnt++;
} else {
if($i==$day) {
$retWeek = $weekCnt;
}
}
}
return $retWeek;
}
echo weekOfMonth('2015-09-08') // gives me 2;
回答by j4r3k
function getWeekOfMonth(DateTime $date) {
$firstDayOfMonth = new DateTime($date->format('Y-m-1'));
return ceil(($firstDayOfMonth->format('N') + $date->format('j') - 1) / 7);
}
Goendg solutiondoes not work for 2016-10-31.
Goendg 解决方案不适用于 2016-10-31。
回答by Marlon Ingal
function weekOfMonth($strDate) {
$dateArray = explode("-", $strDate);
$date = new DateTime();
$date->setDate($dateArray[0], $dateArray[1], $dateArray[2]);
return floor((date_format($date, 'j') - 1) / 7) + 1;
}
weekOfMonth ('2015-09-17') // returns 3
weekOfMonth ('2015-09-17') // 返回 3
回答by Ajay Tambe
You can also use this simple formula for finding week of the month
您还可以使用这个简单的公式来查找一个月中的一周
$currentWeek = ceil((date("d",strtotime($today_date)) - date("w",strtotime($today_date)) - 1) / 7) + 1;
ALGORITHM :
算法 :
Date = '2018-08-08' => Y-m-d
日期 = '2018-08-08' => Ymd
- Find out day of the month eg. 08
- Find out Numeric representation of the day of the week minus 1 (number of days in week) eg. (3-1)
- Take difference and store in result
- Subtract 1 from result
- Divide it by 7 to result and ceil the value of result
- Add 1 to result eg. ceil(( 08 - 3 ) - 1 ) / 7) + 1 = 2
- 找出一个月中的某一天,例如。08
- 找出星期几减去 1(一周中的天数)的数字表示,例如。(3-1)
- 取差异并存储在结果中
- 结果减1
- 将其除以 7 得到结果并限制结果的值
- 将结果加 1,例如。ceil(( 08 - 3 ) - 1 ) / 7) + 1 = 2
回答by Mark Reed
Given the time_t wday (0=Sunday through 6=Saturday) of the first of the month in firstWday
, this returns the (Sunday-based) week number within the month:
给定 中第一个月的 time_t wday(0=周日到 6=周六)firstWday
,这将返回该月内的(基于周日的)周数:
weekOfMonth = floor((dayOfMonth + firstWday - 1)/7) + 1
Translated into PHP:
翻译成PHP:
function weekOfMonth($dateString) {
list($year, $month, $mday) = explode("-", $dateString);
$firstWday = date("w",strtotime("$year-$month-1"));
return floor(($mday + $firstWday - 1)/7) + 1;
}
回答by Andrew Vasiliev
My function. The main idea: we would count amount of weeks passed from the month's first date to current. And the current week number would be the next one. Works on rule: "Week starts from monday" (for sunday-based type we need to transform the increasing algorithm)
我的功能。主要思想:我们将计算从月份的第一个日期到当前日期经过的周数。当前周数将是下一个。工作规则:“一周从星期一开始”(对于基于星期日的类型,我们需要转换递增算法)
function GetWeekNumberOfMonth ($date){
echo $date -> format('d.m.Y');
//define current year, month and day in numeric
$_year = $date -> format('Y');
$_month = $date -> format('n');
$_day = $date -> format('j');
$_week = 0; //count of weeks passed
for ($i = 1; $i < $_day; $i++){
echo "\n\n-->";
$_newDate = mktime(0,0,1, $_month, $i, $_year);
echo "\n";
echo date("d.m.Y", $_newDate);
echo "-->";
echo date("N", $_newDate);
//on sunday increasing weeks passed count
if (date("N", $_newDate) == 7){
echo "New week";
$_week += 1;
}
}
return $_week + 1; // as we are counting only passed weeks the current one would be on one higher
}
$date = new DateTime("2019-04-08");
echo "\n\nResult: ". GetWeekNumberOfMonth($date);
回答by Ivijan Stefan Stipi?
There is a many solutions but here is one my solution that working well in the most cases.
有很多解决方案,但这是我在大多数情况下运行良好的一个解决方案。
function current_week ($date = NULL) {
if($date) {
if(is_numeric($date) && ctype_digit($date) && strtotime(date('Y-m-d H:i:s',$date)) === (int)$date)
$unix_timestamp = $date;
else
$unix_timestamp = strtotime($date);
} else $unix_timestamp = time();
return (ceil((date('d', $unix_timestamp) - date('w', $unix_timestamp) - 1) / 7) + 1);
}
It accept unix timestamp, normal date or return current week from the time()
if you not pass any value.
time()
如果您没有传递任何值,它接受 unix 时间戳、正常日期或返回当前周。
Enjoy!
享受!