1. <small id='OBuBy'></small><noframes id='OBuBy'>

  2. <tfoot id='OBuBy'></tfoot>
    <legend id='OBuBy'><style id='OBuBy'><dir id='OBuBy'><q id='OBuBy'></q></dir></style></legend>
    <i id='OBuBy'><tr id='OBuBy'><dt id='OBuBy'><q id='OBuBy'><span id='OBuBy'><b id='OBuBy'><form id='OBuBy'><ins id='OBuBy'></ins><ul id='OBuBy'></ul><sub id='OBuBy'></sub></form><legend id='OBuBy'></legend><bdo id='OBuBy'><pre id='OBuBy'><center id='OBuBy'></center></pre></bdo></b><th id='OBuBy'></th></span></q></dt></tr></i><div id='OBuBy'><tfoot id='OBuBy'></tfoot><dl id='OBuBy'><fieldset id='OBuBy'></fieldset></dl></div>

        <bdo id='OBuBy'></bdo><ul id='OBuBy'></ul>

      在 PHP5 中将当前日历周的日期作为数组返回

      时间:2023-12-01
      <i id='xeFWp'><tr id='xeFWp'><dt id='xeFWp'><q id='xeFWp'><span id='xeFWp'><b id='xeFWp'><form id='xeFWp'><ins id='xeFWp'></ins><ul id='xeFWp'></ul><sub id='xeFWp'></sub></form><legend id='xeFWp'></legend><bdo id='xeFWp'><pre id='xeFWp'><center id='xeFWp'></center></pre></bdo></b><th id='xeFWp'></th></span></q></dt></tr></i><div id='xeFWp'><tfoot id='xeFWp'></tfoot><dl id='xeFWp'><fieldset id='xeFWp'></fieldset></dl></div>

        <bdo id='xeFWp'></bdo><ul id='xeFWp'></ul>

      • <tfoot id='xeFWp'></tfoot>

            <tbody id='xeFWp'></tbody>

            • <legend id='xeFWp'><style id='xeFWp'><dir id='xeFWp'><q id='xeFWp'></q></dir></style></legend>

              <small id='xeFWp'></small><noframes id='xeFWp'>

                本文介绍了在 PHP5 中将当前日历周的日期作为数组返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我将如何组合一个 PHP5 函数来查找当前日历周并将该周中每一天的日期作为数组返回,从星期一开始?例如,如果函数在今天(2010 年 2 月 25 日星期四)运行,则函数将返回如下数组:

                How would I put together a PHP5 function that would find the current calendar week and return the dates of each day in the week as an array, starting on Monday? For example, if the function were run today (Thu Feb 25 2010), the function would return an array like:

                [0] => Mon Feb 22 2010<br />
                [1] => Tue Feb 23 2010<br />
                [2] => Wed Feb 24 2010<br />
                [3] => Thu Feb 25 2010<br />
                [4] => Fri Feb 26 2010<br />
                [5] => Sat Feb 27 2010<br />
                [6] => Sun Feb 28 2010<br />
                

                日期以什么格式存储在数组中并不重要,因为我认为这很容易更改.此外,最好能够选择提供日期作为参数并获取该日期的日历周而不是当前日历周.

                It doesn't matter what format the dates are stored as in the array, as I assume that'd be very easy to change. Also, it'd be nice to optionally be able to supply a date as a parameter and get the calendar week of that date instead of the current one.

                谢谢!

                推荐答案

                我想一个解决方案是从获取对应于上周一的时间戳开始,使用 strtotime :

                I suppose a solution would be to start by getting the timestamp that correspond to last monday, using strtotime :

                $timestampFirstDay = strtotime('last monday');
                


                但是如果你今天尝试(thursday),像这样:

                $timestampFirstDay = strtotime('last thursday');
                var_dump(date('Y-m-d', $timestampFirstDay));
                

                你会得到:

                string '2010-02-18' (length=10)
                

                即last week... 对于 strtotime,last" 表示 今天之前的那个".

                i.e. last week... For strtotime, "last" means "the one before today".

                这意味着您必须测试 "last monday" 是否是 strtotime 返回的加一星期,如果是,则加一星期...

                Which mean you'll have to test if today is "last monday" as returned by strtotime plus one week -- and, if so, add one week...

                这是一个可能的(可能有更聪明的想法)解决方案:

                Here's a possible (there are probably smarter ideas) solution :

                $timestampFirstDay = strtotime('last monday');
                if (date('Y-m-d', $timestampFirstDay) == date('Y-m-d', time() - 7*24*3600)) {
                    // we are that day... => add one week
                    $timestampFirstDay += 7 * 24 * 3600;
                }
                


                现在我们有了 "last monday" 的时间戳,我们可以编写一个简单的 for 循环,循环 7 次,每次增加 1 天,如下所示:


                And now that we have the timestamp of "last monday", we can write a simple for loop that loops 7 times, adding 1 day each time, like this :

                $currentDay = $timestampFirstDay;
                for ($i = 0 ; $i < 7 ; $i++) {
                    echo date('Y-m-d', $currentDay) . '<br />';
                    $currentDay += 24 * 3600;
                }
                

                这会给我们这样的输出:

                Which will give us this kind of output :

                2010-02-22
                2010-02-23
                2010-02-24
                2010-02-25
                2010-02-26
                2010-02-27
                2010-02-28
                


                现在,由您决定:


                Now, up to you to :

                • 修改 for 循环,使其将日期存储在数组中
                • 决定您要为 date 使用哪种格式 功能
                • Modify that for loop so it stores the dates in an array
                • Decide which format you want to use for the date function

                玩得开心;-)

                这篇关于在 PHP5 中将当前日历周的日期作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在php中获取会议邀请电子邮件的回复 下一篇:如何修改 CodeIgniter 日历以每天处理多个事件?

                相关文章

                <i id='Arp3u'><tr id='Arp3u'><dt id='Arp3u'><q id='Arp3u'><span id='Arp3u'><b id='Arp3u'><form id='Arp3u'><ins id='Arp3u'></ins><ul id='Arp3u'></ul><sub id='Arp3u'></sub></form><legend id='Arp3u'></legend><bdo id='Arp3u'><pre id='Arp3u'><center id='Arp3u'></center></pre></bdo></b><th id='Arp3u'></th></span></q></dt></tr></i><div id='Arp3u'><tfoot id='Arp3u'></tfoot><dl id='Arp3u'><fieldset id='Arp3u'></fieldset></dl></div>

                1. <tfoot id='Arp3u'></tfoot>

                    <legend id='Arp3u'><style id='Arp3u'><dir id='Arp3u'><q id='Arp3u'></q></dir></style></legend>
                      <bdo id='Arp3u'></bdo><ul id='Arp3u'></ul>
                  1. <small id='Arp3u'></small><noframes id='Arp3u'>