获取下一年的星期一和星期二的所有日期 [英] Getting all dates for Mondays and Tuesdays for the next year

查看:82
本文介绍了获取下一年的星期一和星期二的所有日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要输出从当前日期开始的下一个12个月的日期列表(仅星期一和星期二),像这样:

I need to output a list of dates (only Mondays and Tuesdays) for the next 12 months from current date like so:

2010年1月
2010年1月12日,星期二
2010年1月18日星期一
2010年1月19日星期二
2010年1月25日星期一
2010年2月
2010年2月2日,星期二
2010年2月8日星期一
2010年2月9日,周二
2010年2月15日星期一
2010年2月16日,星期二
2010年2月22日星期一
2010年3月
2010年3月9日星期二
2010年3月15日星期一
2010年3月16日星期二
...

Jan 2010
Tue 12 Jan 2010
Mon 18 Jan 2010
Tue 19 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 02 Feb 2010
Mon 08 Feb 2010
Tue 09 Feb 2010
Mon 15 Feb 2010
Tue 16 Feb 2010
Mon 22 Feb 2010
Mar 2010
Tue 09 Mar 2010
Mon 15 Mar 2010
Tue 16 Mar 2010
...

成为PHP的新手,我认为 strtotime 并在接下来的52周内循环播放是最好的方法.

Being new to PHP I figured strtotime and looping over the next 52 weeks is the best way to go.

$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker

// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));

// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) || 
    !in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
     // check if we have to show a new month
     if(strcmp($monthReference, $currentMonth) <> 0){
       echo $monthReference.'<br />',"\n";
     }else{
      // output the dates
      echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";
      echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
     }
       $currentMonth = date("M Y", strtotime('+'.$i.' Week'));
   }
}

但是我的代码输出是

2010年1月
2010年1月18日星期一
2010年1月12日,星期二
2010年1月25日星期一
2010年1月19日星期二
2010年2月
2010年2月8日星期一
2010年2月2日,星期二
2010年2月15日星期一
2010年2月9日,周二
2010年2月22日星期一
2010年2月16日,星期二
2010年3月
2010年3月8日星期一
2010年3月2日星期二
2010年3月15日星期一
2010年3月9日星期二
2010年3月22日星期一
2010年3月16日星期二
2010年3月29日星期一
2010年3月23日,周二

Jan 2010
Mon 18 Jan 2010
Tue 12 Jan 2010
Mon 25 Jan 2010
Tue 19 Jan 2010
Feb 2010
Mon 08 Feb 2010
Tue 02 Feb 2010
Mon 15 Feb 2010
Tue 09 Feb 2010
Mon 22 Feb 2010
Tue 16 Feb 2010
Mar 2010
Mon 08 Mar 2010
Tue 02 Mar 2010
Mon 15 Mar 2010
Tue 09 Mar 2010
Mon 22 Mar 2010
Tue 16 Mar 2010
Mon 29 Mar 2010
Tue 23 Mar 2010

如您所见,日期顺序不正确,在这里我错了,我茫然不知所措.

As you can see the dates are not in the right order and I am at a loss where I am going wrong here.

有没有更优雅/更简单的方法来解决这个问题?

Is there a more elegant / simple way to solve this?

所使用的PHP版本是5.2.11,并且没有希望很快达到5.3的可能性:-(

Version of PHP used is 5.2.11 and no prospect of going to 5.3 anytime soon :-(

感谢您的帮助.

下面是Aly建议的修改代码. 将计算机日期从2010年12月1日星期二更改为2010年13月1日星期三,以测试输出.

Code below modification as suggested by Aly. Changed the computer date from Tue, 12/01/2010 to Wed, 13/01/2010 to test the output.

$blockedDatesInput = "08 Mar 2010,12 Apr 2010"; // dont show these dates
$blockedDates = explode ("," , $blockedDatesInput); // convert to array
$currentMonth = ""; // current month marker

// loop over the next 52 weeks to find Mondays and Tuesdays
for($i=0; $i<=52; $i++){
// build the month header
$monthReference = date("M Y", strtotime('+'.$i.' Week'));

// check if date exists in $blockeddate
if (!in_array(date("d M Y", strtotime('+'.$i.' Monday')), $blockedDates) || 
    !in_array(date("d M Y", strtotime('+'.$i.' Tuesday')), $blockedDates) ) {
     // check if we have to show a new month
     if(strcmp($monthReference, $currentMonth) <> 0){
       echo $monthReference.'<br />',"\n";
     }else{
      // output the dates (changed the order as suggested by Aly)
      echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
      echo date("D d M Y", strtotime('+'.$i.' Monday')).'<br />',"\n";          
     }
       $currentMonth = date("M Y", strtotime('+'.$i.' Week'));
   }
}

以错误的顺序再次输出.

Output again in the wrong order.

2010年1月
2010年1月19日星期二
2010年1月18日星期一
2010年1月26日星期二
2010年1月25日星期一
2010年2月
2010年2月9日,周二
2010年2月8日星期一
2010年2月16日,星期二
2010年2月15日星期一
2010年2月23日,周二
2010年2月22日星期一

Jan 2010
Tue 19 Jan 2010
Mon 18 Jan 2010
Tue 26 Jan 2010
Mon 25 Jan 2010
Feb 2010
Tue 09 Feb 2010
Mon 08 Feb 2010
Tue 16 Feb 2010
Mon 15 Feb 2010
Tue 23 Feb 2010
Mon 22 Feb 2010

推荐答案

这很有趣.这是我对函数的处理方式,尽管它可以保证自己的类确实是模块化的和可重用的:

This is an interesting one. Here's how I'd do it with functions, though it may warrant its own class to really be modular and reusable:

Set up my date formats and excluded dates
define('INTERNAL_FORMAT', 'Y-m-d');
define('DISPLAY_MONTH_FORMAT', 'M Y');
define('DISPLAY_DAY_FORMAT', 'D d M Y');
// format excluded dates as YYYY-MM-DD, date('Y-m-d'):
$excluded_dates = array(
    '2010-03-09',
    '2010-04-13',
);

然后,我需要一些实用程序功能来查看日期如何运行以及排除哪些日期:

Then I need some utility functions to see how the dates run, and what dates are excluded:

// date('w') returns a string numeral as follows:
//   '0' Sunday
//   '1' Monday
//   '2' Tuesday
//   '3' Wednesday
//   '4' Thursday
//   '5' Friday
//   '6' Saturday
function isTuesday($date) {
    return date('w', strtotime($date)) === '2';
}
function isWednesday($date) {
    return date('w', strtotime($date)) === '3';
}

// handle the excluded dates
function isExcludedDate($internal_date) {
    global $excluded_dates;
    return in_array($internal_date, $excluded_dates);
}

现在,我们只需要遍历下一个365天(第二年)的每一天,并检查它们是否在星期二或星期三,而不在排除列表中.我们将其存储在$months_and_dates:

Now we just need to iterate over every day of the next 365 (the next year) and check to see if they're Tuesday or Wednesday and not on the excluded list. We store this in $months_and_dates:

$start_date = date(INTERNAL_FORMAT);

// something to store months and days
$months_and_dates = array();

// loop over 365 days and look for tuesdays or wednesdays not in the excluded list
foreach(range(0,365) as $day) {
    $internal_date = date(INTERNAL_FORMAT, strtotime("{$start_date} + {$day} days"));
    $this_day = date(DISPLAY_DAY_FORMAT, strtotime($internal_date));
    $this_month = date(DISPLAY_MONTH_FORMAT, strtotime($internal_date));
    if ((isTuesday($internal_date) || isWednesday($internal_date)) 
        && !isExcludedDate($internal_date)) {
            $months_and_dates[$this_month][] = $this_day;
    }
}

您可以print_r()它,或者要获得所需的显示,我们可以这样做:

You can print_r() it, or to get the display you want, we do this:

foreach($months_and_dates as $month => $days) {
    print $month . "<br>";
    print implode('<br>', $days);
    print "<br>";
}

这是截至2010年1月11日的结果:

Here's the result as of today, January 11, 2010:

Jan 2010
Tue 12 Jan 2010
Wed 13 Jan 2010
Tue 19 Jan 2010
Wed 20 Jan 2010
Tue 26 Jan 2010
Wed 27 Jan 2010
Feb 2010
Tue 02 Feb 2010
Wed 03 Feb 2010
Tue 09 Feb 2010
Wed 10 Feb 2010
Tue 16 Feb 2010
Wed 17 Feb 2010
Tue 23 Feb 2010
Wed 24 Feb 2010
Mar 2010
Tue 02 Mar 2010
Wed 03 Mar 2010
Wed 10 Mar 2010
Tue 16 Mar 2010
Wed 17 Mar 2010
Tue 23 Mar 2010
Wed 24 Mar 2010
Tue 30 Mar 2010
Wed 31 Mar 2010
Apr 2010
Tue 06 Apr 2010
Wed 07 Apr 2010
Wed 14 Apr 2010
Tue 20 Apr 2010
Wed 21 Apr 2010
Tue 27 Apr 2010
Wed 28 Apr 2010
May 2010
Tue 04 May 2010
Wed 05 May 2010
Tue 11 May 2010
Wed 12 May 2010
Tue 18 May 2010
Wed 19 May 2010
Tue 25 May 2010
Wed 26 May 2010
Jun 2010
Tue 01 Jun 2010
Wed 02 Jun 2010
Tue 08 Jun 2010
Wed 09 Jun 2010
Tue 15 Jun 2010
Wed 16 Jun 2010
Tue 22 Jun 2010
Wed 23 Jun 2010
Tue 29 Jun 2010
Wed 30 Jun 2010
Jul 2010
Tue 06 Jul 2010
Wed 07 Jul 2010
Tue 13 Jul 2010
Wed 14 Jul 2010
Tue 20 Jul 2010
Wed 21 Jul 2010
Tue 27 Jul 2010
Wed 28 Jul 2010
Aug 2010
Tue 03 Aug 2010
Wed 04 Aug 2010
Tue 10 Aug 2010
Wed 11 Aug 2010
Tue 17 Aug 2010
Wed 18 Aug 2010
Tue 24 Aug 2010
Wed 25 Aug 2010
Tue 31 Aug 2010
Sep 2010
Wed 01 Sep 2010
Tue 07 Sep 2010
Wed 08 Sep 2010
Tue 14 Sep 2010
Wed 15 Sep 2010
Tue 21 Sep 2010
Wed 22 Sep 2010
Tue 28 Sep 2010
Wed 29 Sep 2010
Oct 2010
Tue 05 Oct 2010
Wed 06 Oct 2010
Tue 12 Oct 2010
Wed 13 Oct 2010
Tue 19 Oct 2010
Wed 20 Oct 2010
Tue 26 Oct 2010
Wed 27 Oct 2010
Nov 2010
Tue 02 Nov 2010
Wed 03 Nov 2010
Tue 09 Nov 2010
Wed 10 Nov 2010
Tue 16 Nov 2010
Wed 17 Nov 2010
Tue 23 Nov 2010
Wed 24 Nov 2010
Tue 30 Nov 2010
Dec 2010
Wed 01 Dec 2010
Tue 07 Dec 2010
Wed 08 Dec 2010
Tue 14 Dec 2010
Wed 15 Dec 2010
Tue 21 Dec 2010
Wed 22 Dec 2010
Tue 28 Dec 2010
Wed 29 Dec 2010
Jan 2011
Tue 04 Jan 2011
Wed 05 Jan 2011
Tue 11 Jan 2011

这篇关于获取下一年的星期一和星期二的所有日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆