【问题标题】:Get Weekend Dates in php在php中获取周末日期
【发布时间】:2010-07-28 03:58:32
【问题描述】:

我正在开发一个 php 应用程序,它要求我提取两个日期之间的周末日期,然后将它们作为单个记录插入到 mysql 数据库中。

我在想是否有更简单的方法,而不是遍历开始日期和结束日期之间的循环,并为每个日期检查 date('l', strtotime($date)) 是否返回“星期六”或“星期日”

感谢您的宝贵时间

苏尼尔

【问题讨论】:

  • 只是想评论一下,使用 PHP 获得周末约会的可能性很小;o) - waka waka waka。现在我会看看我能为你做些什么。这 2 个日期采用什么格式 - UNIX 更可取?
  • 开个玩笑,Tim……

标签: php date


【解决方案1】:

如果您使用的是较旧的 PHP 安装(或没有 DateTime 类):

$now = strtotime("now");
$end_date = strtotime("+3 weeks");

while (date("Y-m-d", $now) != date("Y-m-d", $end_date)) {
    $day_index = date("w", $now);
    if ($day_index == 0 || $day_index == 6) {
        // Print or store the weekends here
    }
    $now = strtotime(date("Y-m-d", $now) . "+1 day");
}

我们遍历日期范围并检查日期是否为0 或6 索引(星期日或星期六)。

【讨论】:

  • 谢谢。我将 startdate 转换为时间并使用上述函数 'while (date("Y-m-d", $start_date) != date("Y-m-d", $end_date)) { $day_index = date("w", $start_date); if ($day_index == 0 || $day_index == 6) { echo date('Y-m-d',$start_date)."
    "; } $start_date = strtotime(date("Y-m-d", $start_date) . "+1 day"); }'
【解决方案2】:

好吧,php Date("N") 为您提供星期几,其中 1 是星期一,7 是星期日,因此您可以很容易地将其实现到 if 语句中。

【讨论】:

    【解决方案3】:
    $now = new DateTime("now");
    $now->setTime(0,0);
    if (($now->format("l") == "Saturday") || ($now->format("l") == "Sunday"))
        $d = $now;
    else
        $d = new DateTime("next saturday");
    
    $oneday = new DateInterval("P1D");
    $sixdays = new DateInterval("P6D");
    $res = array();
    while ($d->getTimestamp() <= $endTimestamp) {
        $res[] = $d->format("Y-m-d");
        $d = $d->add($oneday);
        if ($d->getTimestamp() <= $endTimestamp) {
            $res[] = $d->format("Y-m-d");
        }
        $d = $d->add($sixdays);
    }
    

    示例

    $end = new DateTime("2010-08-20");
    $endTimestamp = $end->getTimestamp();
    
    数组(6){ [0]=> 字符串(10)“2010-07-31” [1]=> 字符串(10)“2010-08-01” [2]=> 字符串(10)“2010-08-07” [3]=> 字符串(10)“2010-08-08” [4]=> 字符串(10)“2010-08-14” [5]=> 字符串(10)“2010-08-15” }

    【讨论】:

      【解决方案4】:

      这没有经过很好的测试,但它似乎工作得很好。请注意,如果 $start 是在周末,而 $end 是在一周中的同一天但更早,则 $end 表示的日期将被省略 - 因此时间戳最好是在午夜。

      <?php
      $start = $now = 1280293200; // starting time
      $end = 1283014799; // ending time
      $day = intval(date("N", $now));
      $weekends = array();
      if ($day < 6) {
        $now += (6 - $day) * 86400;
      }
      while ($now <= $end) {
        $day = intval(date("N", $now));
        if ($day == 6) {
          $weekends[] += $now;
          $now += 86400;
        }
        elseif ($day == 7) {
          $weekends[] += $now;
          $now += 518400;
        }
      }
      echo "Weekends from " . date("r", $start) . " to " . date("r", $end) . ":\n";
      foreach ($weekends as $timestamp) {
        echo date("r", $timestamp) . "\n";
      }
      

      【讨论】:

      • 为什么不使用strtotime?如果您的幻数输入有误会怎样?
      • 我想我假设时间戳都是UTC,以避免此类问题。
      【解决方案5】:

      对于发现这个的其他人 - 我使用了以下内容:

      $start = new DateTime($this->year.'-'.$month.'-01');
      $interval = new DateInterval('P1D');
      $end = new DateTime($this->year.'-'.$month.'-31');
      
      $period = new DatePeriod($start,$interval,$end);
      
      foreach ($period as $day){
          if ($day->format('N') == 6 || $day->format('N') == 7){
              $compulsory[$day->format('d')] = true;
          }
      }
      

      修改您自己的开始范围和结束范围,而不是仅格式化 $compulsory 中的 d - 您可以使用 d/m/Y 来获得更重要的日期。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-02-18
        • 1970-01-01
        • 2016-05-02
        • 2022-01-17
        • 1970-01-01
        • 1970-01-01
        • 2019-12-18
        • 1970-01-01
        相关资源
        最近更新 更多