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 中将当前日历周的日期作为数组返回

      Return dates of current calendar week as array in PHP5(在 PHP5 中将当前日历周的日期作为数组返回)
      <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 中将当前日历周的日期作为数组返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以
                PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的
                mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)
                Laravel Gmail Configuration Error(Laravel Gmail 配置错误)
                Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)
                Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)

                  <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>

                        <tbody id='Arp3u'></tbody>

                    • <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>
                        • <small id='Arp3u'></small><noframes id='Arp3u'>