7

如何找到每月的第 2 和第 4 个星期六。我写了这些行:-

echo "may 2nd sat ".date('d', strtotime('may 2013 second saturday'));
echo '<br/>may 4th sat '.date('d', strtotime('may 2013 fourth saturday'));                                  
echo '<br/>june 2nd sat '.date('d', strtotime('june 2013 second saturday'));
echo '<br/>june 4th sat '.date('d', strtotime('june 2013 fourth saturday'));

它提供以下输出:-

may 2nd sat 11
may 4th sat 25
june 2nd sat 15
june 4th sat 29

它在月份给出正确答案 may但不是june 2013,在jun 1013第 2 和第 4 个星期六应该分别是 8 和 22。我怎么能解决这个问题。

4

7 回答 7

7

我不确定为什么你的不起作用,但试试这个,它对我有用:

echo '<br/>june 2nd sat '.date('d', strtotime('second sat of june 2013'));
echo '<br/>june 4th sat '.date('d', strtotime('fourth sat of june 2013'));

它基于PHP 手册页"first sat of July 2008"中的示例

于 2013-06-11T06:53:17.850 回答
4

我不知道为什么会导致此错误,但是我找到了如何获得正确答案的解决方案

echo date('d',strtotime('+1 week sat may 2013')).'<BR>';
echo date('d',strtotime('+3 week sat may 2013')).'<BR>';
echo date('d',strtotime('+1 week sat june 2013')).'<BR>';
echo date('d',strtotime('+3 week sat june 2013')).'<BR>';

此解决方案工作正常并显示正确的结果。

输出:

11
25
08
22
于 2013-06-11T06:55:26.467 回答
1

这些天我首选的方法是扩展 PHP 的DateTime 对象:-__

class MyDateTime extends DateTime
{
    /**
     * Returns a MyDateTime object set to 00:00 hours on the nth occurence
     * of a given day of the month
     *
     * @param string $n nth day required, eg first, second etc
     * @param string $day Name of day
     * @param mixed $month Month number or name optional defaults to current month
     * @param mixed $year optional defaults to current year
     *
     * @return MyDateTime set to last day of month
     */
    public function nthDayOfMonth($n, $day, $month = null, $year = null)
    {
        $timestr = "$n $day";
        if(!$month) $month = $this->format('M');
        $timestr .= " of $month $year";
        $this->setTimestamp(strtotime($timestr));
        $this->setTime(0, 0, 0);
        return $this;
    }
}
$dateTime = new MyDateTime();
echo $dateTime->nthDayOfMonth('second', 'Sun', 'Jul', 2011)->format('Y-m-d');

输出:-

2011-07-10
于 2013-06-11T06:54:55.400 回答
1

然后您必须运行低于 5.2.7 版本的 PHP 版本,如手册所述

在 5.2.7 之前的 PHP 5 中,如果该工作日是该月的第一天,则请求在一个月内出现给定的工作日会错误地将一周添加到返回的时间戳。这已在 5.2.7 及更高版本中得到纠正。

因此,如果您可以更新您的 PHP 版本,那将是最好的解决方案。否则你可以检查类似的东西。

<?php
function showDay($month, $year, $day, $count)
{
  $list = array(1=>'first',2=>'second',3=>'third',4=>'fourth',5=>'fifth');
  $first = date('d', strtotime($month . ' ' . $year . ' ' . $list[1] .' '.$day));
  $show= ($first>7) ?  $count-1 : $count;
  return date('d', strtotime($month . ' ' . $year . ' ' . $list[$show] .' '.$day));
}

echo '<br/>june 2nd sat '.showDay('june', 2013, 'saturday', 2);
?>
于 2013-06-11T06:56:30.533 回答
1

我通常不依赖一根绳子。定制功能怎么样?

function getSaturdayDay($year, $month, $position) {
    $firstDay = date('w', mktime(0, 0, 0, $month, 1, $year));
    $diff = 6 - $firstDay;

    return 1 + $diff + $position * 7;
}

并在您的上下文中使用它

echo "may 2nd sat " . getSaturdayDay(2013, 5, 1);
echo '<br/>may 4th sat ' . getSaturdayDay(2013, 5, 3);                          
echo '<br/>june 2nd sat ' . getSaturdayDay(2013, 6, 1);
echo '<br/>june 4th sat ' . getSaturdayDay(2013, 6, 3);
于 2013-06-11T06:58:13.793 回答
0

查找每月的第二个和第四个星期六:

private bool FindSecondSaturdayFourthSaturday()
{      
    bool IsHoliday = false;
    DateTime TodaysDate = DateTime.Today;            
    DateTime SecondSaturday;
    DateTime FourthSaturday;

    //SecondSaturday will be after 8
    SecondSaturday = Enumerable.Range(8, 7)
                  .Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
                  .Where(date => date.DayOfWeek == DayOfWeek.Saturday)
                  .Single();
    //Lsat Saturday will be after 22 
    FourthSaturday = Enumerable.Range(22, 7)
                 .Select(item => new DateTime(TodaysDate.Year, TodaysDate.Month, item))
                 .Where(date => date.DayOfWeek == DayOfWeek.Saturday)
                 .Single();

    return IsHoliday;
}
于 2018-11-14T16:27:35.823 回答
0

每年获得第二个和第三个星期六

public function getSndFthSaturday()
    {
        $now = strtotime("01-01-2019");
        $end_date = strtotime("31-12-2019");
        $this->calculate($now, $end_date);
    }

    public function calculate($now, $end_date)
    {
        while (date("Y-m-d", $now) != date("Y-m-d", $end_date))
        {
            $day_index = date("w", $now);
            $day_indexD = floor((date("d", $now) - 1)/ 7);

            if ($day_index == 6 && ($day_indexD == 1 || $day_indexD == 3)) {
                $now1 = date("Y-m-d", $now);
                print_r($now1);
                echo "</br>";
            }
            $now = strtotime(date("Y-m-d", $now) . "+1 day");
        }
    }
于 2019-02-14T13:15:48.180 回答