Find second and fourth saturday's of the month
Asked Answered
H

7

7

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.

Harbert answered 11/6, 2013 at 6:42 Comment(0)
A
7

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

Adult answered 11/6, 2013 at 6:53 Comment(1)
The above link doesn't work me. Use this one instead: ca1.php.net/manual/en/datetime.formats.relative.phpAcrylonitrile
H
4

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
Hoot answered 11/6, 2013 at 6:55 Comment(0)
A
1

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
Arginine answered 11/6, 2013 at 6:54 Comment(0)
R
1

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);
?>
Responsion answered 11/6, 2013 at 6:56 Comment(1)
The OP is tagged as php-5.4. But it's useful information anyway, good to knowAdult
V
1

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);
Voyles answered 11/6, 2013 at 6:58 Comment(1)
the most reliable answer by farGiavani
E
0

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;
}
Essential answered 14/11, 2018 at 16:27 Comment(0)
B
0

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");
        }
    }
Blotter answered 14/2, 2019 at 13:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.