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

问题描述 投票:7回答: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。我怎么能解决这个问题。

php datetime php-5.4
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'));

它基于"first sat of July 2008"中的PHP manual page示例


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

1
投票

然后你必须运行低于版本5.2.7的PHP版本,作为manual states

在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);
?>

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

1
投票

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

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

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;
}

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");
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.