php 取上周二,php – 获取明年周一和周二的所有日期

这是一个有趣的.这是我如何使用函数来完成它,虽然它可以保证它自己的类真正是模块化和可重用的:

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',);

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

// 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中:

$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()它,或者为了获得你想要的显示,我们这样做:

foreach($months_and_dates as $month => $days) {

print $month . "
";

print implode('
',$days);

print "
";

}

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值