如何找到每月的查找第一个星期日

时间:2012-12-24 10:17:22

标签: php

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

请帮帮我。

getSunday();

4 个答案:

答案 0 :(得分:10)

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

echo date("Y-m-d", strtotime("first Sunday of ".date('M')." ".date('Y').""));

答案 1 :(得分:8)

您可以使用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
*/

答案 2 :(得分:5)

我正在寻找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'));

答案 3 :(得分: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));
            }
        }
    }