问题描述
我有随机时间(不是从MySQL推出)的数组。我需要将它们分组按周为Week1,Week2等高达Week5。
我已经是这样的:
$日期=阵列('2015年9月1日,2015年9月5日,2015年9月6日,2015年9月15日, 2015年9月17日');
我需要的是一个函数提供的日期以获得该月的周数。
我知道我可以做得到weeknumber
日期('W',的strtotime(2015年9月1日'));
但本周号码是一年之间(1-52)的数量,但我需要一个月而已,例如星期数在2015年9月有5周:
- Week1 = 1日至5日
- Week2 = 6日至12日
- 译员更加= 13日至19日
- Week4 = 20日至26日
- Week5 = 27日至30日
我应该能够通过只是提供日期以一周Week1
例如。
$ weekNumber = getWeekNumber('2015年9月1日)//输出1;
$ weekNumber = getWeekNumber('2015年9月17日)//输出3;
我觉得这种关系应该是真实的,并派上用场(?):
月=周在今年的周 - 月+ 1的第一天的年周
在PHP实现你得到这样的:
函数weekOfMonth($日期){
//获取该月的第一天。
$ firstOfMonth =的strtotime(日期(Y-M-01,$日期));
//应用上面的公式。
返回INTVAL(日期(W,$日期)) - INTVAL(日期(W,$ firstOfMonth))+ 1;
}
要获取星期日开始几周,只需更换这两个日期(W,...)
与的strftime(%U ,...)
。
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.
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:
- Week1 = 1st to 5th
- Week2 = 6th to 12th
- Week3 = 13th to 19th
- Week4 = 20th to 26th
- Week5 = 27th to 30th
I should be able to get the week Week1 by just providing the datee.g.
$weekNumber = getWeekNumber('2015-09-01') //output 1;
$weekNumber = getWeekNumber('2015-09-17') //output 3;
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:
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", ...)
.
这篇关于获取周数在一个月日期PHP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!