-3

I have require first sunday date of every month using php code.

please can help me.

getSunday();
Piyush Viradiya
  • 401
  • 1
  • 3
  • 10

4 Answers4

14

This is the best way to get first Sunday of the month

echo date("Y-m-d", strtotime("first Sunday of ".date('M')." ".date('Y').""));
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
Manish Patel
  • 1,877
  • 1
  • 14
  • 15
8

You can use the Date/Time extension.

$start    = new DateTime('2012-12-31');
$end      = new DateTime('2013-12-31');
$interval = DateInterval::createFromDateString('first sunday of next month');
$period   = new DatePeriod($start, $interval, $end, DatePeriod::EXCLUDE_START_DATE);
foreach($period as $time) {
    echo $time->format("F jS") . "<br>\n";
}
/*
January 6th 
February 3rd 
March 3rd 
April 7th 
May 5th 
June 2nd 
July 7th 
August 4th 
September 1st 
October 6th 
November 3rd 
December 1st
*/
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
6

I was looking for the DTS dates so here's how I found them:

$dtsStart = date('Y-m-d 02:00:00', strtotime('Second Sunday Of March 2015'));
$dtsEnd = date('Y-m-d 02:00:00', strtotime('First Sunday Of November 2015'));
phoenix
  • 1,629
  • 20
  • 11
3
function firstSunday($DATE)
    {
        $date = strftime("%Y-%m",$DATE);
        $day = trim(strftime("%e",$DATE));

        for($day; $day <= '7'; $day++){
            $dd = strftime("%A",strtotime($date.'-'.$day));
            if($dd == 'Sunday'){
                return strftime("%Y-%m-%d",strtotime($date.'-'.$day));
            }
        }
    }
Manish Ambalia
  • 359
  • 1
  • 8
mahesh patel
  • 134
  • 6
  • Use [`getdate()`](http://php.net/manual/en/function.getdate.php) or [`date('w')`](http://php.net/manual/en/function.date.php) to learn about the weekday of the first day in the month then add or subtract the correct number of days and that's all. You don't need to check all the days in the week. – axiac Apr 10 '15 at 17:03