为获取周一和周二的所有日期为明年

问题描述 投票:9回答:4

我需要输出时间(只有星期一和星期二),从当前日期,像这样的未来12个月的列表:

2010年1月 星期二2010年1月12日 我2010年1月18日 星期二2010年1月19日 我2010年1月25日 2010年2月 周二02 2010年2月 我的2010 08二月 周二09 2010年2月 我2010-2-16 周二16 2010年2月 周一22 2010年2月 2010年3月 星期二2010年3月9日, 我2010年3月15日 星期二2010年3月16日 ...

作为新PHP我想通的strtotime和循环在未来52周是最好的一段路要走。

$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 08二月 周二02 2010年2月 我2010-2-16 周二09 2010年2月 周一22 2010年2月 周二16 2010年2月 2010年3月 我的8 2010年3月 星期二2010年3月2 我2010年3月15日 星期二2010年3月9日, 我2010年3月22日, 星期二2010年3月16日 我2010年3月29日 星期二2010年3月23日

正如你所看到的日期是不是在正确的顺序,我很茫然我要去的地方错在这里。

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

使用PHP的版本是5.2.11,并没有将5.3短期前景:-(

谢谢你的帮助。

如阿里建议如下修改代码。改变从星期二,12/01/2010到星期三,13/01/2010计算机日期到测试输出。

$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'));
   }
}

再次输出顺序错误。

2010年1月 星期二2010年1月19日 我2010年1月18日 星期二2010年1月26日 我2010年1月25日 2010年2月 周二09 2010年2月 我的2010 08二月 周二16 2010年2月 我2010-2-16 周二23 2010年2月 周一22 2010年2月

php strtotime
4个回答
12
投票

这是一个有趣的一个。下面是我如何与功能做到这一点,尽管它可能保证自己的类要真正模块化和可重用:

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 . "<br>";
    print implode('<br>', $days);
    print "<br>";
}

这里的结果是今天,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星期一”是下周一,而不是今天。

你可能想看看进入“N”格式字符日期()。


0
投票

现在好了,你的计算机上的日期是周三你想要的星期二之前打印的周一为下周一比下周二接近至周三。所以,试试这个:

$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.' Monday')).'<br />',"\n";          
      echo date("D d M Y", strtotime('+'.$i.' Tuesday')).'<br />',"\n";
     }
       $currentMonth = date("M Y", strtotime('+'.$i.' Week'));
   }
}

0
投票
<?php
/*
    @$listDaysOfWeek list days
    0 = Monday
    1 = Tuesday
    2 = Wednesday
    3 = Thursday
    4 = Friday
    5 = Saturday
    6 = Sunday
*/

function DatesFromDaysOnWeek(\Datetime $objDateTime, int $numAddDays , array $listDaysOfWeek)
{
    $listDaysOfWeek     = array_intersect($listDaysOfWeek, range(0,6) );
    sort($listDaysOfWeek);

    if(empty($listDaysOfWeek) || $numAddDays < 1)
        return [];

    $results = [];
    while ( $numAddDays > 0)
    {
        foreach($listDaysOfWeek as $numDay)
        {
            $infoDate = (object) getdate( $objDateTime->getTimestamp() );
            if($numDay != $infoDate->wday)
                continue;

            array_push($results, $objDateTime->format('Y-m-d') );
            $numAddDays--;
        }

        $objDateTime->add(new \DateInterval('P1D'));
    }

    return $results;
}

#example:
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [1] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [2] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [3] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [4] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [5] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [6] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-02-01')) , 4 , [0] );
print_r($list);
$list = DatesFromDaysOnWeek( (new \DateTime('2019-03-01')) , 100 , [1,2] );
print_r($list);
© www.soinside.com 2019 - 2024. All rights reserved.