【问题标题】:Get date for monday and friday for the current week (PHP)获取本周星期一和星期五的日期(PHP)
【发布时间】:2009-09-16 08:15:06
【问题描述】:

如何获取本周的星期一和星期五的日期?

我有以下代码,但如果当天是星期天或星期六,它会失败。

$current_day = date("N");
$days_to_friday = 5 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("Y-m-d", strtotime("- {$days_from_monday} Days"));
$friday = date("Y-m-d", strtotime("+ {$days_to_friday} Days"));

【问题讨论】:

  • 你把星期天算作一周的开始还是结束?

标签: php date datetime


【解决方案1】:

最好的解决办法是:

$monday = date( 'Y-m-d', strtotime( 'monday this week' ) );
$friday = date( 'Y-m-d', strtotime( 'friday this week' ) );

【讨论】:

  • 这比公认的答案要好得多。 “本周五”和“下周五”没有区别。您必须使用“本周星期五”和“下周星期五”
  • 谢谢。 “本周” - 正是我在此类问题中搜索的内容。
  • “本周”是什么意思?从星期一开始的一周还是从星期日开始的一周?您无法分辨,因为它取决于 PHP 版本。请参阅 3v4l.org/v1ZI7并单击 EOL 版本。一些 7.x 版本会产生意想不到的结果。
【解决方案2】:

我使用:

$first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
$last_week_date = date('d F Y', strtotime('next Monday -1 week + 4 days', strtotime('this sunday')));

【讨论】:

    【解决方案3】:

    这些 strtotime 输入效果很好:

    strtotime( "next monday" );
    strtotime( "previous monday" );
    strtotime( "today" );
    strtotime( "next friday" );
    strtotime( "previous friday" );
    

    您需要做的就是将逻辑包装在一些 if 语句中。

    【讨论】:

    • 不错的答案,但我想从 date 星期一得到什么。因为 01-01-2012 将返回 2-01-2012
    • @chhameed:strtotime("next monday", strtotime("2012-01-01"))。第二个参数(如果指定)用作参考日期。
    • 如果当前是 Monday 而我们调用 strtotime( "previous monday") 会发生什么?
    • @kelly 用户应该检查今天的星期几并根据结果使用 prev 或下星期一。当我发布这个答案时,strtotime 的一周处理被严重破坏了。同样 AFAIK,PHP 不允许您指定一周的第一天......而且没有人知道 PHP 认为什么是一周的开始。
    • @SalmanA 我可以看到。 Php 是一种非常不稳定的语言。
    【解决方案4】:

    这个问题需要一个日期时间的答案:-

    /**
     * @param String $day
     * @return DateTime
     */
    function getDay($day)
    {
        $days = ['Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Sunday' => 7];
    
        $today = new \DateTime();
        $today->setISODate((int)$today->format('o'), (int)$today->format('W'), $days[ucfirst($day)]);
        return $today;
    }
    

    用法:

    var_dump(getDay('Monday')->format('l dS F Y'));
    var_dump(getDay('Friday')->format('l dS F Y'));
    

    输出:

    string 'Monday 30th September 2013' (length=26)
    string 'Friday 04th October 2013' (length=24)
    

    See it working

    【讨论】:

      【解决方案5】:

      正如上面的答案所暗示的,使用 PHP 的 strtotime() 函数是最简单的方法。

      但是,不要像他建议的那样使用 if 语句,您可以简单地重置回上一个星期日并从那里获取您需要的日期。

      $monday = strtotime('next monday', strtotime('previous sunday'));
      $friday = strtotime('next friday', strtotime('previous sunday'));
      

      【讨论】:

        【解决方案6】:

        这实际上取决于您如何定义一周,但我想出了这个函数,它可以为您提供最近的“星期一”或“星期五”(或任何一天)的日期:

        function closestDate($day){
        
            $day = ucfirst($day);
            if(date('l', time()) == $day)
                return date("Y-m-d", time());
            else if(abs(time()-strtotime('next '.$day)) < abs(time()-strtotime('last '.$day)))
                return date("Y-m-d", strtotime('next '.$day));
            else
                return date("Y-m-d", strtotime('last '.$day));
        
        }
        

        输入:一周中的某一天(“星期日”、“星期一”等)

        输出:如果我问最近的“星期天”,今天是:

        1. “星期日”:我会得到今天的日期
        2. “星期一”:我会得到昨天的日期
        3. “星期六:我会得到明天的日期

        希望这会有所帮助:)

        【讨论】:

          【解决方案7】:

          我需要根据 ISO 8601 定义当前周。我希望将星期一始终定义为本周开始的星期一。

          以下解决方案非常适合我:

          $monday = strtotime(date('o-\WW'));
          $friday = strtotime("next friday",$monday);
          

          对于$monday,此方法将始终返回本日历周开始的星期一。不幸的是,这种方法依赖于 PHP 5.1 来解析 o 日期格式。

          要获得一周中的任何一天,您可以尝试:

          function time_for_week_day($day_name, $ref_time=null){
              $monday = strtotime(date('o-\WW',$ref_time));
              if(substr(strtoupper($day_name),0,3) === "MON")
                  return $monday;
              else
                  return strtotime("next $day_name",$monday);
          }
          

          用法:

          time_for_week_day('wednesday');
          time_for_week_day('friday',strtotime('2014-12-25'));
          

          【讨论】:

            【解决方案8】:

            我一直在寻找类似的东西,除了我想要任何星期一,而不仅仅是本周。这是我想出的:

            function getSunday(DateTime $date){
                $outdate = clone($date);
                $day = $date->format("w");  // get the weekday (sunday is 0)
                $outdate->sub(new DateInterval("P".$day."D")); // subtracting the weekday from the date always gives Sunday 
                return $outdate;
            }
            

            它接受任意日期并给出星期日。然后,您可以轻松地加回星期一到星期六的日期。

            【讨论】:

              【解决方案9】:

              获取当前周

                      $week = [];
                      $saturday = strtotime('saturday this week');
                      foreach (range(0, 6) as $day) {
                          $week[] = date("Y-m-d", (($day * 86400) + $saturday));
                      }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-11-27
                • 1970-01-01
                • 1970-01-01
                • 2018-06-25
                • 2014-07-24
                相关资源
                最近更新 更多