如何从 PHP 中的两个日期范围中提取每周一和每两周的周一?

发布于 2024-11-17 19:16:02 字数 471 浏览 5 评论 0原文

我正在使用臭名昭著的 jQuery UI 的日期选择器,并在我的表单中选择两个日期的范围。

第一个代表开始日期,另一个代表结束日期。

我现在需要的是计算这两个日期之间每周一的算法、一些提示和方向或帮助者。

例如:

start: 2011-06-01
end:   2011-06-30

应该为我提取周一的这四(四)个日期:

1st: 2011-06-06
2nd: 2011-06-13
3rd: 2011-06-20
4th: 2011-06-27

我怎样才能实现这一点?

而且,我每两周星期一都需要它:

每两周的结果应该是:

1st: 2011-06-06
2rd: 2011-06-20

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一页 2024-11-24 19:16:02
$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

现在您的所有星期一都在 $mondays 数组中。

附录

请注意,由于夏令时,+86400 可能会导致结果不一致。如果您的应用是关键任务,最好使用 +1 天

strtotime('2010-10-31') + 86400 返回 2010-10-31 >

$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

又怨 2024-11-24 19:16:02
function getAllMondays($from_date, $to_date){
// getting number of days between two date range.
$number_of_days = count_days(strtotime($from_date),strtotime($to_date));

for($i = 1; $i<=$number_of_days; $i++){
    $day = Date('l',mktime(0,0,0,date('m'),date('d')+$i,date('y')));
    if($day == 'Monday'){
        echo Date('d-m-Y',mktime(0,0,0,date('m'),date('d')+$i,date('y'))),'<br/>';
    }
}

}

function getAllMondays($from_date, $to_date){
// getting number of days between two date range.
$number_of_days = count_days(strtotime($from_date),strtotime($to_date));

for($i = 1; $i<=$number_of_days; $i++){
    $day = Date('l',mktime(0,0,0,date('m'),date('d')+$i,date('y')));
    if($day == 'Monday'){
        echo Date('d-m-Y',mktime(0,0,0,date('m'),date('d')+$i,date('y'))),'<br/>';
    }
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文