如何在两个时间间隔之间获得星期五日期

时间:2017-12-31 18:17:14

标签: php laravel

我想从两个时间间隔得到所有星期五的日期。 我的框架是laravel。

我的职能:

public function getFridaysInRange($dateFromString, $dateToString)
{
    $dateFrom = new \DateTime($dateFromString);
    $dateTo = new \DateTime($dateToString);
    $dates = [];

    if ($dateFrom > $dateTo) {
        return $dates;
    }

    if (1 != $dateFrom->format('N')) {
        $dateFrom->modify('next friday');
    }

    while ($dateFrom <= $dateTo) {
        $dates[] = $dateFrom->format('Y-m-d');
        $dateFrom->modify('+1 week');
    }

    return $dates;
}

但是从它返回的两个日期开始:

"["2017-12-25","2018-01-01","2018-01-08","2018-01-15","2018-01-22","2018-01-29","2018-02-05","2018-02-12","2018-02-19","2018-02-26","2018-03-05","2018-03-12","2018-03-19","2018-03-26","2018-04-02","2018-04-09","2018-04-16","2018-04-23","2018-04-30","2018-05-07","2018-05-14","2018-05-21","2018-05-28","2018-06-04","2018-06-11","2018-06-18","2018-06-25","2018-07-02","2018-07-09","2018-07-16","2018-07-23","2018-07-30","2018-08-06","2018-08-13","2018-08-20","2018-08-27","2018-09-03","2018-09-10","2018-09-17","2018-09-24","2018-10-01","2018-10-08","2018-10-15","2018-10-22","2018-10-29","2018-11-05","2018-11-12","2018-11-19","2018-11-26","2018-12-03","2018-12-10","2018-12-17","2018-12-24","2018-12-31"]"

我的问题在哪里?

3 个答案:

答案 0 :(得分:2)

你可以使用Carbon:

$dateTo = Carbon::parse($dateToString);
$friday = Carbon::parse($dateFromString . ' next friday');
$fridays = [];

while($friday->lt($dateTo)) {
    $fridays[] = $friday->toDateString();
    $friday->addWeek();
}

return $fridays;

答案 1 :(得分:1)

尝试以下功能:

function getWeekDays($start, $end, $weekday)
{
    $start = strtotime($start);
    $end = strtotime($end);

    $weekdays = array();

    while (date("w", $start) != $weekday) {
        $start += 86400;
    }

    while ($start <= $end)
    {
        $weekdays[] = date('Y-m-d', $start);
        $start += 604800;
    }

    return($weekdays);
}

示例:

// get all fridays between 1st November 2017 and 1st December 2017
print_r(getWeekDays('2017-11-01','2017-12-01', 5));

输出:

Array
(
    [0] => 2017-11-03
    [1] => 2017-11-10
    [2] => 2017-11-17
    [3] => 2017-11-24
    [4] => 2017-12-01
)

访问this link以测试脚本的完整工作演示。

答案 2 :(得分:1)

您似乎对您的问题不感兴趣,但您的原始代码很容易修复:

<?php

function getFridaysInRange($dateFromString, $dateToString)
{
    $dateFrom = new \DateTime($dateFromString);
    $dateTo = new \DateTime($dateToString);
    $dates = [];

    if (5 != $dateFrom->format('N')) {
        $dateFrom->modify('next friday');
    }

    if ($dateFrom > $dateTo) {
        return $dates;
    }

    while ($dateFrom <= $dateTo) {
        $dates[] = $dateFrom->format('Y-m-d');
        $dateFrom->modify('+1 week');
    }

    return $dates;
}

var_dump(getFridaysInRange('2017-11-28', '2018-03-31'));

主要问题是你在星期几不断检查,应该是5(假设你正在检查星期五)。

我还重新检查了dateFrom&gt;的支票。 dateTo,因为一旦你修改了dateFrom,你实际上可以将它推到dateTo之外,这就是你想要抓住它的目的。

输出:

array(18) {
  [0]=>
  string(10) "2017-12-01"
  [1]=>
  string(10) "2017-12-08"
  [2]=>
  string(10) "2017-12-15"
  [3]=>
  string(10) "2017-12-22"
  [4]=>
  string(10) "2017-12-29"
  [5]=>
  string(10) "2018-01-05"
  [6]=>
  string(10) "2018-01-12"
  [7]=>
  string(10) "2018-01-19"
  [8]=>
  string(10) "2018-01-26"
  [9]=>
  string(10) "2018-02-02"
  [10]=>
  string(10) "2018-02-09"
  [11]=>
  string(10) "2018-02-16"
  [12]=>
  string(10) "2018-02-23"
  [13]=>
  string(10) "2018-03-02"
  [14]=>
  string(10) "2018-03-09"
  [15]=>
  string(10) "2018-03-16"
  [16]=>
  string(10) "2018-03-23"
  [17]=>
  string(10) "2018-03-30"
}