-3

我需要使用 php 代码每个月的第一个星期日日期。

请帮帮我。

getSunday();
4

4 回答 4

14

这是获得每月第一个星期日的最佳方式

echo date("Y-m-d", strtotime("first Sunday of ".date('M')." ".date('Y').""));
于 2013-10-05T08:37:35.980 回答
8

您可以使用日期/时间扩展

$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
*/
于 2012-12-24T10:49:44.357 回答
6

我正在寻找 DTS 日期,所以这是我找到它们的方法:

$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'));
于 2015-04-10T16:58:01.957 回答
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));
            }
        }
    }
于 2012-12-24T10:20:16.643 回答