【问题标题】:Show dates of this week显示本周日期
【发布时间】:2015-07-10 17:24:52
【问题描述】:

我正在寻找一种方法来显示当前星期的日期或过去的日期,也许使用周数?

我试过了

echo date('d/m/Y', strtotime('previous monday'));

但显然这不适用于当前日期或未来日期

任何人都可以帮忙 - 我是新手! 谢谢

【问题讨论】:

  • 我不确定我是否理解你的问题。你的实际目标是什么?您想要一周中的当前日期吗?
  • 请编辑它,它很混乱!
  • 对不起,我不知道该怎么说,我想从周数中获取一周中每一天的日期?
  • 您输入了周数,想要获取 mo、tue、wed、thur 和 friday 的日期?
  • @Reflic - 没错

标签: php date


【解决方案1】:

如果你想要一周的日期,你可以使用这个代码

$week_start = new DateTime();
$week_start->setISODate($year,$week_no);
echo $week_start->format('d-M-Y');

我没有测试过它,但我认为它会给你一个星期一的日期,你可以从这个日期出发,而不是去其他日子。

来源:http://www.lornajane.net/posts/2011/getting-dates-from-week-numbers-in-php

【讨论】:

    【解决方案2】:

    这就是我要做的。基本上找到最后一个星期天(除非目标日期是星期天),然后循环过去直到星期六。

    <?php
    //our target timestamp to get the week
    // $targetDate = time(); //today
    $targetDate = strtotime('1-Jan-2015'); //specific date
    
    //if target day is not day 0 (sunday)
    if(date('w', $targetDate) > 0) {
        //get the last sunday
        $sunday = strtotime('last sunday', $targetDate);
    } else {
        //target day is sunday, get target date
        $sunday = $targetDate;
    }
    
    //if you want shorthand ternary statement, this would work for finding sunday. I wrote it out above so it is easier to understand:
    // $sunday = date('w', $targetDate) ? strtotime('last sunday', $targetDate) : $targetDate;
    
    //get the following saturday as our stop point
    $saturday = strtotime('saturday', $sunday);
    
    //loop over each day of the week
    for(
        $currentDay = $sunday;      //start on sunday
        $currentDay <= $saturday;   //while we aren't on saturday yet
        $currentDay = strtotime('+1 day', $currentDay) //add one day after each loop
    ){
        //echo out current day's date
        echo date('d-M-Y', $currentDay).'<br>';
    }
    

    example

    我个人更喜欢这个,因为您还可以在循环中执行其他操作,例如回显特定日期的某些值。

    【讨论】:

      【解决方案3】:

      下面的sn-p应该根据你输入的星期给你前几天:

      $weeks = 1;
      $weekdays = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'];
      $first_day_current_year = mktime(0, 0, 0, 1, 1, date('Y', time())); // d/m/Y H:i:s => 01/01/YYYY 00:00:00
      $n_weeks_after = strtotime('+ '. $weeks . ' week', $first_day_current_year); // d/m/Y H:i:s => 08/01/YYYY 00:00:00
      $result = ['reference date' => date('l, d/m/Y, H:i:s', $n_weeks_after)];
      
      foreach ($weekdays as $day) {
        $weekday = strtotime('previous ' . $day, $n_weeks_after);
        $result['previous ' . $day] = date('l, d/m/Y, H:i:s', $weekday);
      }
      
      echo '<pre>';
      print_r($result);
      echo '</pre>';
      

      在这个例子中,我设置了$weeks = 1,所以它的范围是从 1 月 1 日(星期四)到 1 月 7 日(星期三)。 下面的输出:

      Array
      (
          [reference date] => Thursday, 08/01/2015, 00:00:00
          [previous monday] => Monday, 05/01/2015, 00:00:00
          [previous tuesday] => Tuesday, 06/01/2015, 00:00:00
          [previous wednesday] => Wednesday, 07/01/2015, 00:00:00
          [previous thursday] => Thursday, 01/01/2015, 00:00:00
          [previous friday] => Friday, 02/01/2015, 00:00:00
          [previous saturday] => Saturday, 03/01/2015, 00:00:00
          [previous sunday] => Sunday, 04/01/2015, 00:00:00
      )
      

      对于当前一周,您可以将$weeks 设置为:

      $weeks = date('W', time()); // ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)
      

      你可以在这里测试它:http://www.writephponline.com/

      【讨论】:

        【解决方案4】:

        这是一个使用周数获​​取日期的函数。
        第一个参数是周数(1=一年中的第一周,模式设置为 ISO),
        第二个参数是工作日索引(0-6,0=星期一,6=星期日)
        前三个参数是可选的,否则设置为当前值。
        如果没有任何参数,您将以您在问题中使用的格式获得当前日期。
        如果您将第二个参数(工作日)设置为 7(或更高),您将获得一周中所有日期的数组。
        使用“H:i”格式,您可以看到夏令时的偏移量,例如夏季为“01:00”,否则为“00:00”。

        function getWeekDate($week=-1, $weekday=-1, $year=-1, $format="d/m/Y", $mode="ISO") {
            // default is the current
            if($week < 0) { $week = date("W"); }
            if($weekday < 0) { $weekday = date("w")-1; }
            if($weekday < 0) { $weekday+= 7; } // on sundays
            if($year < 0) { $year = date("Y"); }
            $time = strtotime("1 January $year", time());
            $day = date('w', $time);
            if($mode=="ISO") {
                // ISO 8601: first week of the year is the week with the first thursday
                if($day<=4) { $day+= 7; }
            }
            $time+= ((7*$week)+1-$day)*24*3600;
            $return = array();
            for($i=0;$i<=6;$i++) {
                $return[$i] = date($format, $time);
                $time+= 24*3600;
            }
            if(isset($return[$weekday])) {
                $return = $return[$weekday];
            }
            return $return;
        }
        
        // Examples:
        echo "<br>Current Date: ".getWeekDate();
        echo "<br>Monday of the Current Week: ".getWeekDate(-1,0);
        echo "<br>Sunday of the Next Week: ".getWeekDate(date("W")+1,6);
        echo "<br>All Days of Week 28: ".print_r(getWeekDate(28,7),true);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-03-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多