如何从一个范围在PHP中两个日期中提取每星期一,每双周星期一? [英] How to extract every Monday and every fortnightly Monday from a range of two dates in PHP?
问题描述
我使用的是臭名昭著的jQuery UI的的日期选择器,并在我的形式我选择一个范围内的两个日期。
第一重presents在开始日期,另一个重presents在结束日期。
我现在需要的是algorthm,一些提示和指导或助手来计算每个星期一这两个日期之间。
例如:
启动:2011-06-01
结束:2011-06-30
如果提取我这些4(4件)日期女巫躺在星期一:
1日:2011-06-06
第二:2011-06-13
第三:2011-06-20
4日:2011-06-27
我怎么能achive呢?
和也,我需要它为每双周星期一:
结果为每两周应该是:
1日:2011-06-06
2RD:2011-06-20
$开始=的strtotime('2011-06-01');
$结束=的strtotime('2011-06-30');
$星期一=阵列();
而($开始< = $结束){
如果(日期(N,$开始)== 1)
$星期一[] = $启动;
$启动+ = 86400; //>递增1天
//>我可以用的strtotime('+1天),但数学总和是10倍的速度
}
//>未测试
现在你在 $周一所有的周一
阵列。
附录
请注意, 86400
可能导致不一致的结果,由于夏令时。如果你的应用程序是关键任务更好地利用1天
的strtotime('2010-10-31')+ 86400
返回 2010-10-31
I'm using the infamous jQuery UI's Datepicker, and in my form I select a range of two dates.
First represents the starting date and the other represents the end date.
What I need now is the algorthm, some tips and directions or helpers for calculating every Monday between these two dates.
For instance:
start: 2011-06-01
end: 2011-06-30
Should extract me these 4 (four) dates witch lay on Mondays:
1st: 2011-06-06
2nd: 2011-06-13
3rd: 2011-06-20
4th: 2011-06-27
How could I achive this?
And also, I'd need it for every fortnightly Monday:
Result for fortnightly should be:
1st: 2011-06-06
2rd: 2011-06-20
$start = strtotime('2011-06-01');
$end = strtotime('2011-06-30');
$mondays=array();
while( $start <= $end ) {
if ( date('N',$start)==1 )
$mondays[]=$start;
$start += 86400; //> incrementing one day
//> i could have used strtotime('+1 day') but math sum is 10x faster
}
//> Untested
Now you have all your mondays in $mondays
array.
Addendum
Be aware that +86400
could lead to inconsistent result due to daylight saving. If your app is mission-critical better use +1 day
strtotime('2010-10-31') + 86400
returns 2010-10-31
这篇关于如何从一个范围在PHP中两个日期中提取每星期一,每双周星期一?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!