Home » Php » php – Find second and fourth saturday's of the month

php – Find second and fourth saturday's of the month

Posted by: admin July 12, 2020 Leave a comment

Questions:

How to find 2nd and 4th Saturday of the month.
I wrote these lines:-

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

It gives the following output :-

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

It gives correct answer in may month but not june 2013, in jun 1013 2nd and 4th saturday should be 8 and 22 respectively. How could I solve this issue.

How to&Answers:

I’m not sure why yours won’t work, but try this, it worked for me:

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

It’s based on the "first sat of July 2008" example in the PHP manual page

Answer:

I don’t know why it is causing this error howver I’ve found solution how to get correct answers

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

This solution works fine and shows proper results.

Output:

11
25
08
22

Answer:

Then you must be running a PHP version below version 5.2.7, as the manual states

In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

So if you can update your PHP version that would be the best solution. Else you can check something like.

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

Answer:

I don’t usually rely on a piece of string. How about a custom-made function?

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

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

and use it in your context

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

Answer:

These days my preferred method is to extend PHP’s 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');

Output:-

2011-07-10

Answer:

Find second and fourth Saturday’s of the month:

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

Answer:

Get second and third saturday yearly

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