如何在一个月内显示星期日? [英] How to display sundays in a month?

查看:125
本文介绍了如何在一个月内显示星期日?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图得到这个问题的答案,一些R& D我也提出了解决方案。

I have been trying to get the answer for this question and after some R&D i have come up with the solution too

$begin = new DateTime('2014-11-01');
$end = new DateTime('2014-11-30');
$end = $end->modify('+1 day');
$interval = new DateInterval('P1D');
$daterange = new DatePeriod($begin, $interval, $end);

foreach ($daterange as $date) {
    $sunday = date('w', strtotime($date->format("Y-m-d")));
    if ($sunday == 0) {
        echo $date->format("Y-m-d") . "<br>";
    } else {
        echo'';
    }
}


推荐答案

是当月显示的所有星期日的另一种方法:

This is another method for shown all sundays in current month:

<?php
    function getSundays($y, $m)
{
    return new DatePeriod(
        new DateTime("first sunday of $y-$m"),
        DateInterval::createFromDateString('next sunday'),
        new DateTime("last day of $y-$m")
    );
}

foreach (getSundays(2014, 11) as $sunday) {
    echo $sunday->format("l, Y-m-d\n");
}
?>

参考这个 Codepad.viper

这篇关于如何在一个月内显示星期日?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆